Python: SSLError, bad handshake, Unexpected EOF

Viewed 16284

I have an issue with connecting to a specific site using Python requests and get this error:

HTTPSConnectionPool(host='XXXXXXXXX', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))

How can I work around this? (setting verify=False does not make a difference) I'm suspecting the server who's at fault here as it get an overall rating of F @ ssllabs when I run their test

I am fairly new with Python and requests

my code:

import requests
try:
    site = requests.get('https://XXXXXXXXXX', verify=True)
    print(site)
except requests.exceptions.RequestException as e:
    print(e)
    pass
4 Answers

The root cause might be this open bug in the requests library: "Session.verify=False ignored when REQUESTS_CA_BUNDLE environment variable is set".

We've seen similar issues start all of a sudden on a specific host. It turned out that the env variable was set there recently, which started causing requests to spawn with session.verify not False despite being initialized to False. Once we removed the REQUESTS_CA_BUNDLE environment variable the errors stopped.

Set verify = False, it will help:

import requests
try:
    site = requests.get('https://XXXXXXXXXX', verify=False)
    print(site)
except requests.exceptions.RequestException as e:
    print(e)
    pass

or try with urllib:

import requests
import urllib.request
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

try:
    site =urllib.request.urlopen(url, context=ctx)
    print(site.read().decode())
except requests.exceptions.RequestException as e:
    print(e)
    pass
Related