How to get Python requests to trust a self signed SSL certificate?

Matthew Moisen picture Matthew Moisen · May 22, 2015 · Viewed 119.4k times · Source
import requests
data = {'foo':'bar'}
url = 'https://foo.com/bar'
r = requests.post(url, data=data)

If the URL uses a self signed certificate, this fails with

requests.exceptions.SSLError: [Errno 1] _ssl.c:507: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

I know that I can pass False to the verify parameter, like this:

r = requests.post(url, data=data, verify=False)

However, what I would like to do is point requests to a copy of the public key on disk and tell it to trust that certificate.

Answer

krock picture krock · May 22, 2015

try:

r = requests.post(url, data=data, verify='/path/to/public_key.pem')