Python 3.5 - 连接被对端重置
Python 3.5 - Connection reset by peer
我看到 "Connection reset by peer" 和 Python 有很多不同的问题,但是,我认为这个问题是不同的。
问题是:我正在尝试通过 https 访问与我在同一网络中的 Web 服务。我通过 Python 执行调用的每一次尝试都以 "Connection reset by peer" 返回。我是 运行 这个 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)
有什么想法吗?
我想通了。
我目前的假设是默认的 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)
我看到 "Connection reset by peer" 和 Python 有很多不同的问题,但是,我认为这个问题是不同的。
问题是:我正在尝试通过 https 访问与我在同一网络中的 Web 服务。我通过 Python 执行调用的每一次尝试都以 "Connection reset by peer" 返回。我是 运行 这个 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)
有什么想法吗?
我想通了。
我目前的假设是默认的 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)