我正在使用requests.get(导入请求)通过连接到某个HTTP端点来刮取一些指标。
我可以在手动运行命令时运行连接到端点 -
>>> 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.
当我尝试通过我的python脚本运行相同的命令时,我得到以下错误 -
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
我的代码片段 -
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如下所示 -
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"
你能帮忙吗?
答案 0 :(得分:0)
当你在函数调用中插入字符串时,字符串的python repr与requests.get()调用不兼容。这就是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'"
将字符串转换为字节:
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
你应该好好去。