简体   繁体   English

ConnectionError :(“连接已中止。”,gaierror(-2,“名称或服务未知”))

[英]ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))

I am using requests.get (import requests) to scrape some metrics by connecting to some HTTP endpoint. 我正在使用request.get(导入请求)通过连接到某些HTTP端点来抓取某些指标。

I am able to run connect to the endpoints while running the command manually - 我能够在手动运行命令的同时运行连接到端点-

>>> import requests
>>>
>>> output = requests.get("http://10.206.124.139:9209/metrics/8ebab4dd-  84bd-48c2-998c-aade88d8c82c/46567dbe-24d0-4cca-b432-a28ac5e831ec/0e297dfe-c64d-4139-bb14-a884c3e1ebc9/dbd5ca46-de73-4fe0-8273-9a9a0f5faa7a/549b0b1a-d3cc-47f0-8917-3919cb432aa6")
>>> output.text   ---> I am able to see the output.

When I try to run the same command via my python script i get the below error- 当我尝试通过python脚本运行相同的命令时,出现以下错误-

raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service   not known'))

Snippet of my Code - 我的代码段-

target_end_point = target_end_point.replace("127.0.0.1:10090",    self.replace_string)
metrics = requests.get('http://%s' % target_end_point)

target_end_point looks like below when i print it - 当我打印时,target_end_point如下所示-

final target endpoint is           "10.206.124.139:9209/metrics/8ebab4dd-84bd-48c2-998c-aade88d8c82c/46567dbe-24d0-4cca-b432-a28ac5e831ec/0e297dfe-c64d-4139-bb14-a884c3e1ebc9/dbd5ca46-de73-4fe0-8273-9a9a0f5faa7a/549b0b1a-d3cc-47f0-8917-3919cb432aa6"

Can you please help? 你能帮忙吗?

When you insert string into function calls, python repr of the string isn't compatible wit the requests.get() call. 当您将字符串插入函数调用时,该字符串的python repr与request.get()调用不兼容。 This is what python feeds into the call (notice the extra quotes): 这就是python输入到调用中的内容(请注意额外的引号):

repr(target_endpoint) # -->"'127.0.0.1:10090/metrics/8ebab4dd-84bd-48c2-998c-aade88d8c82c/46567dbe-24d0-4cca-b432-a28ac5e831ec/0e297dfe-c64d-4139-bb14-a884c3e1ebc9/dbd5ca46-de73-4fe0-8273-9a9a0f5faa7a/549b0b1a-d3cc-47f0-8917-3919cb432aa6'"

Convert the string to bytes: 将字符串转换为字节:

target_end_point = target_end_point.replace("127.0.0.1:10090",    self.replace_string)
target_end_point = target_end_point.encode() # encode the variable
metrics = requests.get(b'http://' + target_end_point) # change the 'http:// to a bytestring

And you should be good to go. 而且您应该很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 requests.exceptions.ConnectionError :('Connection aborted。',gaierror(8,'nodename或servname提供,或未知')) - requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(8, 'nodename nor servname provided, or not known')) Python Spider ConnectionError :(“连接已中止。”,BadStatusLine(“''”,)) - Python Spider ConnectionError: ('Connection aborted.', BadStatusLine(“''”,)) msg:ConnectionError(ProtocolError('Connection aborted。',error(2,'No such file or directory')),) - msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),) Web 抓取 - 请求 ConnectionError: ('Connection aborted.', OSError(“(60, 'ETIMEDOUT')”,)) - Web Scraping - Requests ConnectionError: ('Connection aborted.', OSError(“(60, 'ETIMEDOUT')”,)) Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) - Python ConnectionError: ('Connection aborted.', OSError(“(10060, 'WSAETIMEDOUT')”)) ConnectionError :('Connection aborted。',BrokenPipeError(32,'Broken pipe')) - ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe')) gaierror:[Errno -2]姓名或服务未知 - gaierror: [Errno -2] Name or service not known socket.gaierror gaierror:[Errno -2]名称或服务未知-pika rabbitMQ - socket.gaierror gaierror: [Errno -2] Name or service not known - pika rabbitMQ socket.gaierror:[错误-2]名称或服务未知 - socket.gaierror: [Error -2] Name or service not known socket.gaierror:[Errno -2]名称或服务未知 - socket.gaierror: [Errno -2] Name or service not known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM