Python 3.5 - 通过对等方重置连接

时间:2016-01-28 20:58:24

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

我已经看到了很多不同的问题"连接由同行重置"然而,我认为这个问题不同。

问题在于:我正在尝试通过https访问与我的网络相同的网络服务。我通过Python执行调用所做的每一次尝试都返回了"连接由同行重置"。我在Linux上运行它。

但是,我可以通过浏览器Curl并访问Web服务而不会有任何麻烦。此外,这个相同的脚本适用于Windows和其他Linux环境(尽管那些运行Python 2.7)。

我已经尝试了两个请求并使用了基础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)

任何想法?

1 个答案:

答案 0 :(得分:0)

我明白了。

我目前的假设是默认的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)