0

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!

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
dcampos
  • 149
  • 1
  • 6
  • 2
    FYI The server has [bad TLS support](https://www.ssllabs.com/ssltest/analyze.html?d=www.gasnaturalfenosa.com). – Klaus D. Aug 02 '18 at 20:42
  • I've rolled back your edit where you've removed the URL since the information which site you are trying to access is essential for answering the question. And the problem is not TLS 1.0 as you've tried to suggest in your edit. – Steffen Ullrich Aug 03 '18 at 14:42

1 Answers1

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')
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Thanks for your answer! But I'm sorry to say that this doesn't work for me due to "requests.packages.urllib3.exceptions.SSLError: ('No cipher can be selected.',)" exception. I looked out for this but for now I still facing this issue.. – dcampos Aug 03 '18 at 06:39
  • 1
    @dcampos: it worked for me though. It might be that you are using a version of OpenSSL where support for 3DES is disabled as some newer Linux distributions do. In this case, no luck with the Python you have installed - maybe try another Python installation like Anaconda Python. – Steffen Ullrich Aug 03 '18 at 14:37