[英]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.