问题
I was trying to download a PDF file from Internet and Python2.7.15cr1 and requests 2.19.1 but I am facing this error:
> 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')],)",),))
The code that I'm using to trying to download a PDF file is:
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)
If I try to download it using curl I got the same error. I've been trying to fix this error for several days, so I'll be very grateful if someone could give me a hint of why this is happening!!
Thank you in advance!
回答1:
This server is hopelessly broken. According to the SSLLabs report it only supports TLS 1.0 and only insecure or weak ciphers using DES or 3DES. These ciphers are disabled by default in requests.
If you still want to connect to the server you explicitly need to allow the weak 3DES cipher. As already described in Requests failing to connect to a TLS server this works like this:
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')
来源:https://stackoverflow.com/questions/51661796/bad-handshake-when-using-requests