【问题标题】:Uppercase URL in requests returns "Name does not resolve"请求中的大写 URL 返回“名称未解析”
【发布时间】:2019-04-10 08:13:46
【问题描述】:

我想从带有大写字符的 URL 获取数据。 URL 基于 docker 主机名。 requests 总是返回Name does not resolve,因为它降低了 URL。

网址是http://gateway.Niedersachsen/api/bundeslaender。

ping gateway.Niedersachsen 有效,但 ping gateway.niedersachsen 无效。

我的 Python 请求代码:

url = f'http://gateway.Niedersachsen/api/wfs/insertGeometry'
r = requests.get(url)

出现以下错误:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='gateway.niedersachsen', port=80): Max retries exceeded with url: /api/wfs/insertGeometry (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f5f5eb5a3c8>: Failed to establish a new connection: [Errno -2] Name does not resolve'))

我的版本:

$ python --version
Python 3.7.3

> requests.__version__
'2.21.0'

【问题讨论】:

  • 用它的IP替换gateway.Niedersachsen怎么样?
  • 由于网关服务的扩展,这是没有选择的。多个服务导致多个 IP ????

标签: python url python-requests


【解决方案1】:

RFC 3986 第 6.2.2.1 节谈到了 URI:

[...] 方案和 主机 不区分大小写,因此应标准化为小写 [...]。

恕我直言,您的名称解析行为不正确,似乎有一个开放的issue 与 Docker 网络的区分大小写相关,我假设这里正在使用它。

requests,分别。 urllib3,尊重 RFC 建议,至少对于 HTTP 方案连接。就requests 而言,似乎有四个相关的地方将主机名转换为小写。

  1. urllib3 的实用程序类 Url 在 requests' PreparedRequest 实例执行 prepare_url 方法时发挥作用。
  2. PoolManager 通过key_fn_by_scheme 映射调用的_default_key_normalizer 函数
  3. 如果您的主机名包含非 ASCII 字符,它也是 passed through IDNA 编码,但在您的示例中并非如此。
  4. urllib3 1.22 版还在ConnectionPool 基类初始化程序中对主机名进行了lower() 调用。从 1.23 版开始,这种规范化显然已移至 _ipv6_host 函数。

使用monkeypatching,我似乎能够强制requests,resp。 urllib3,保持 URL 的主机名部分不变:

import functools
import urllib3

def _custom_key_normalizer(key_class, request_context):
    # basically a 1:1 copy of urllib3.poolmanager._default_key_normalizer
    # commenting out 
    # https://github.com/urllib3/urllib3/blob/master/src/urllib3/poolmanager.py#L84
    #context['host'] = context['host'].lower()

class ConnectionPool(object):
    def __init__(self, host, port=None):
        # complete copy of urllib3.connectionpool.ConnectionPool base class
        # I needed this due to my urllib3 version 1.22. 
        # If you have urllib3 >= 1.23 this is not necessary
        # remove the .lower() from 
        # https://github.com/urllib3/urllib3/blob/1.22/urllib3/connectionpool.py#L71
        self.host = urllib3.connectionpool._ipv6_host(host)

urllib3.util.url.NORMALIZABLE_SCHEMES = (None,)
# This is needed for urllib3 >= 1.23. The connectionpool module imports
# NORMALIZABLE_SCHEMES before we can patch it, so we have to explicitly patch it again
urllib3.connectionpool.NORMALIZABLE_SCHEMES = (None,)
urllib3.poolmanager.key_fn_by_scheme['http'] = functools.partial(_custom_key_normalizer, 
                                                                 urllib3.poolmanager.PoolKey)
# just for urllib3 < 1.23
urllib3.connectionpool.ConnectionPool = ConnectionPool

# do not use anything that would import urllib3 before this point    
import requests
url = f'http://gateway.Niedersachsen/api/wfs/insertGeometry'
r = requests.get(url)

我的错误消息显示连接池中使用的主机仍然使用初始大写,因此我假设成功:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='gateway.Niedersachsen', port=80): [...]

注意:
直接使用urllib3 可能有更简单的方法;我还没有研究过这个。
另外,如果有人知道使用requests 保留主机大小写的更直接的方法,请告诉我。

【讨论】:

  • 看起来不错,但我无法运行它。我使用的是urllib3 1.24.1 版,因此省略了您的一些代码。我使用通常和prepared requests 进行调试,我准备的请求具有正确的(大写)URL。然而,实际请求失败并出现相同的初始错误。不知道怎么回事……
  • @Felix 我错过了 ConnectionPool 基类中的规范化在 urllib3 版本 1.23 中已移动,而不是删除。您还需要在connectionpool 模块的级别上修补NORMALIZABLE_SCHEMES:urllib3.connectionpool.NORMALIZABLE_SCHEMES = (None,)。我更新了我的答案。
猜你喜欢
  • 2017-02-20
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 2019-03-29
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多