【问题标题】:Python 3.5 - Connection reset by peerPython 3.5 - 对等方重置连接
【发布时间】:2016-01-28 20:58:24
【问题描述】:

我已经看到很多关于“对等连接重置”和 Python 的不同问题,但是,我认为这个问题是不同的。

问题是:我正在尝试通过 https 访问与我在同一网络中的 Web 服务。我通过 Python 执行调用的每一次尝试都返回了“对等方重置连接”。我在 Linux 上运行它。

但是,我可以通过浏览器进行 Curl 和访问 Web 服务,没有任何麻烦。此外,同样的脚本也适用于 Windows 和其他 Linux 环境(尽管它们运行的​​是 Python 2.7)。

我已经尝试了这两个请求并使用了 base urllib.request。

# I specify blank proxy since I have CNTLM setup for outbound.
requests.get(url, proxies={'https': ''}, auth=(user, pass), verify=False)

我也试过了:

session = requests.Session()
session.trust_env=False

# This still brings up 'Connection reset by peer' with/without http auth
response = session.get(url)

我已经尝试了很长的路......

import urrlib
proxy_handler = urllib.request.ProxyHandler({})
proxy_opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(proxy_opener)

# do the same for basic auth....

resp = urllib.request.urlopen(url)

有什么想法吗?

【问题讨论】:

    标签: python-3.x python-requests urllib


    【解决方案1】:

    我想通了。

    我目前的假设是默认 SSL/TLS 协议是服务器不接受的协议。基本上意味着服务器需要更新以使用最新版本的 TLS。

    但是,Requests 和 Urllib 并未解决该连接问题。我已指定要在传输中使用的旧版本 SSL(我知道...我知道...)。

    我的代码最终类似于下面的which was derived from the requests documentation

    import certifi
    import ssl
    import requests
    
    from requests.adapters import HTTPAdapter
    from requests.packages.urllib3.poolmanager import PoolManager
    
    class Ssl3HttpAdapter(HTTPAdapter):
        def init_poolmanager(self, connections, maxsize, block=False):
            # specify the version of SSL you want to use below
            self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize, block=block, 
                                           ssl_version=ssl.PROTOCOL_SSLv3)
    
    s = requests.Session()
    
    # May or may not need this. I needed it since I didn't need to route through a proxy to get to local services.
    s.trust_env = False
    
    s.verify = certifi.where()
    s.auth = (user, password)  # Basic-Auth username/password
    
    s.mount(base_url_of_your_service, Ssl3HttpAdapter())
    
    req = s.get(your_service_url)
    print(req.text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多