Python3.6.8 urllib.request.urlopen错误“对等连接重置”

问题描述

我正在尝试使用urllib.request.urlopen从maps.googleapis.com获取地理编码信息。

我面临一个奇怪的问题,其中urllib.request.urlopen在启用代理服务器的情况下工作,但在没有代理的情况下无法工作。

在ssl hadnshake期间出现连接重置错误,

Traceback (most recent call last):
  File "/usr/lib64/python3.6/urllib/request.py",line 1318,in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib64/python3.6/http/client.py",line 1254,in request
    self._send_request(method,url,body,headers,encode_chunked)
  File "/usr/lib64/python3.6/http/client.py",line 1300,in _send_request
    self.endheaders(body,encode_chunked=encode_chunked)
  File "/usr/lib64/python3.6/http/client.py",line 1249,in endheaders
    self._send_output(message_body,line 1036,in _send_output
    self.send(msg)
  File "/usr/lib64/python3.6/http/client.py",line 974,in send
    self.connect()
  File "/usr/lib64/python3.6/http/client.py",line 1415,in connect
    server_hostname=server_hostname)
  File "/usr/lib64/python3.6/ssl.py",line 365,in wrap_socket
    _context=self,_session=session)
  File "/usr/lib64/python3.6/ssl.py",line 773,in __init__
    self.do_handshake()
  File "/usr/lib64/python3.6/ssl.py",line 1033,in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib64/python3.6/ssl.py",line 645,in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/usr/lib64/python3.6/urllib/request.py",line 223,in urlopen
    return opener.open(url,data,timeout)
  File "/usr/lib64/python3.6/urllib/request.py",line 526,in open
    response = self._open(req,data)
  File "/usr/lib64/python3.6/urllib/request.py",line 544,in _open
    '_open',req)
  File "/usr/lib64/python3.6/urllib/request.py",line 504,in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.6/urllib/request.py",line 1361,in https_open
    context=self._context,check_hostname=self._check_hostname)
  File "/usr/lib64/python3.6/urllib/request.py",line 1320,in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 104] Connection reset by peer>

我在requests模块上遇到了类似的问题,但是当我如下更新时,我能够使用requests.get方法获得响应,

pip install requests[security]

我已与我们的网络安全团队核对,并将maps.googleapis.com的所有IP列入白名单。

我用cURL命令检查了它的工作情况,也很好。

有人知道urllib.request.urlopen是什么问题吗?

我正在使用python 3.6.8版本。

解决方法

1)自动机器人检测机制最有可能断开您的连接。您应该提供User-Agent标头来伪造浏览器访问。

headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/55.0.2883.95 Safari/537.36'})
  1. 在安装请求库时,它会跳过SSL / Https连接所需的一些可选安全软件包(“ pyOpenSSL”,“ ndg-httpsclient”和“ pyasn1”)。您可以通过运行以下命令来修复它

    pip install "requests[security]"
    

pip install pyopenssl ndg-httpsclient pyasn1

如果它不起作用

  1. 您可以(有时)通过策略性地放置time.sleep(0.01)来纠正此问题。

请参阅此https://stackoverflow.com/a/383816/13126651

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...