nebbul
nebbul

Reputation: 141

Python: SSLError, bad handshake, Unexpected EOF

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

Upvotes: 14

Views: 20035

Answers (4)

Roshin Raphel
Roshin Raphel

Reputation: 2709

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

Upvotes: 0

G. Sh.
G. Sh.

Reputation: 11

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.

Upvotes: 0

okainov
okainov

Reputation: 4664

As mentioned in other question https://stackoverflow.com/a/36499718/1657819 this error may happen if you're under proxy, so disabling proxy may help

unset https_proxy

Upvotes: 1

bryn
bryn

Reputation: 3325

Faced with the same error my troubles went away after doing pip install ndg-httpsclient. yum install python-ndg_httpsclient or apt-get install python-ndg-httpsclient (or apt-get install python3-ndg-httpsclient) probably works too.

Upvotes: 2

Related Questions