Garrett Smith
Garrett Smith

Reputation: 696

Python requests "certificate verify failed"

I'm maintaining a Python mini-app that uses requests + HTTPS.

The app worked until the IP address of the hostname in the HTTPS URL changed (legitimately). If I point my browser to the URL I can retrieve it fine.

Where does Python/requests keep the analog of ssh's known_hosts and how do I clear it for this host?

$ python --version
Python 2.7.3

$ cat foo.py 
import requests
url = "https://somehost/resource.json"
requests.get(url, timeout=5, config={'danger_mode': True})

$ source venv/bin/activate
$ python foo.py 
Traceback (most recent call last):
  File "foo.py", line 3, in <module>
    requests.get(url, timeout=5, config={'danger_mode': True})
  File "/home/dfukdev/corsair-scripts/alfred/venv/local/lib/python2.7/site-packages/requests/api.py", line 65, in get
return request('get', url, **kwargs)
  File "/home/dfukdev/corsair-scripts/alfred/venv/local/lib/python2.7/site-packages/requests/safe_mode.py", line 39, in wrapped
return function(method, url, **kwargs)
  File "/home/dfukdev/corsair-scripts/alfred/venv/local/lib/python2.7/site-packages/requests/api.py", line 51, in request
return session.request(method=method, url=url, **kwargs)
  File "/home/dfukdev/corsair-scripts/alfred/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 241, in request
r.send(prefetch=prefetch)
  File "/home/dfukdev/corsair-scripts/alfred/venv/local/lib/python2.7/site-packages/requests/models.py", line 641, in send
raise SSLError(e)
requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL     routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Upvotes: 15

Views: 33875

Answers (3)

Christopher
Christopher

Reputation: 11

If the above doesn't work, and you find out it is local, then this solution worked for me...

Essentially run the Install Certificates.command file within Python folder.

https://www.dev2qa.com/how-to-fix-python-error-certificate-verify-failed-unable-to-get-local-issuer-certificate-in-mac-os/

Upvotes: 0

Garrett Smith
Garrett Smith

Reputation: 696

It turned out that during the server upgrade mentioned in the question an incorrectly-signed certificate was installed. HTTPS in the browser worked because of the root certificate differences between the Windows browser machine and Ubuntu Python client. HTTPS via a browser from the same Ubuntu machine on which Python was run revealed the certificate problem details.

The IP change had little to do with the problem except to confuse things.

Promoting my comment to an answer as:

  1. this answered my question
  2. this question is getting enough traffic I'd like to share the knowledge.

Upvotes: 2

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28707

You're using an ancient version of requests. You'll get a more helpful message if you upgrade to 2.0 and if your site has a certificate mismatch you may be able to fix it by specifying the system certificates which will be able to verify an intermediate certificate. You can also just have requests not verify your certificate as Andre suggested.

Upvotes: 6

Related Questions