【问题标题】:Bad Handshake when using requests使用请求时握手错误
【发布时间】:2019-01-10 16:59:27
【问题描述】:

我试图从 Internet 和 Python2.7.15cr1 下载 PDF 文件并请求 2.19.1,但我遇到了这个错误:

>     Traceback (most recent call last):
>       File "download.py", line 5, in <module>
>         r = requests.get(url,verify=False)
>       File "/home/user/.local/lib/python2.7/site-packages/requests/api.py",
> line 72, in get
>         return request('get', url, params=params, **kwargs)
>       File "/home/user/.local/lib/python2.7/site-packages/requests/api.py",
> line 58, in request
>         return session.request(method=method, url=url, **kwargs)
>       File "/home/user/.local/lib/python2.7/site-packages/requests/sessions.py",
> line 512, in request
>         resp = self.send(prep, **send_kwargs)
>       File "/home/user/.local/lib/python2.7/site-packages/requests/sessions.py",
> line 622, in send
>         r = adapter.send(request, **kwargs)
>       File "/home/user/.local/lib/python2.7/site-packages/requests/adapters.py",
> line 511, in send
>         raise SSLError(e, request=request)
>     requests.exceptions.SSLError: HTTPSConnectionPool(host='host', port=443): Max
> retries exceeded with url: /en/files/pdftodownload.pdf
> (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines',
> 'ssl3_read_bytes', 'sslv3 alert handshake failure')],)",),))

我用来尝试下载 PDF 文件的代码是:

import requests

url = 'https://www.gasnaturalfenosa.com/en/files/GasNaturalSDG_ing_2016-2.pdf'
r = requests.get(url, stream=True)

with open('metadata.pdf', 'wb') as fd:
    for chunk in r.iter_content(chunk_size=2048):
        fd.write(chunk)

如果我尝试使用 curl 下载它,我会遇到同样的错误。 我已经尝试修复这个错误好几天了,所以如果有人能告诉我为什么会发生这种情况,我将不胜感激!

提前谢谢你!

【问题讨论】:

  • 仅供参考服务器有bad TLS support
  • 我已经回滚了您删除 URL 的编辑,因为您尝试访问的站点信息对于回答问题至关重要。问题不在于您尝试在编辑中建议的 TLS 1.0。

标签: python ssl openssl python-requests sslhandshakeexception


【解决方案1】:

这台服务器彻底坏了。根据SSLLabs report,它仅支持 TLS 1.0,并且仅支持使用 DES 或 3DES 的不安全或弱密码。默认情况下,这些密码在请求中被禁用。

如果您仍想连接到服务器,您需要明确允许弱 3DES 密码。正如Requests failing to connect to a TLS server 中已经描述的那样:

import requests 
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = 'DES-CBC3-SHA' 
resp=requests.get('https://www.gasnaturalfenosa.com/en/files/GasNaturalSDG_ing_2016-2.pdf')

【讨论】:

  • 感谢您的回答!但我很遗憾地说,由于“requests.packages.urllib3.exceptions.SSLError: ('No cipher can be selected.',)”异常,这对我不起作用。我留意了这个,但现在我仍然面临这个问题..
  • @dcampos:不过它对我有用。可能是您使用的 OpenSSL 版本像某些较新的 Linux 发行版一样禁用了对 3DES 的支持。在这种情况下,您安装的 Python 不走运 - 不妨尝试其他 Python 安装,例如 Anaconda Python。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多