【问题标题】:ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))
【发布时间】:2018-05-07 16:04:49
【问题描述】:

我正在使用 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"

你能帮忙吗?

【问题讨论】:

    标签: python python-requests


    【解决方案1】:

    当您将字符串插入函数调用时,字符串的 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
    

    你应该很高兴。

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2022-06-15
      • 2016-03-12
      相关资源
      最近更新 更多