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

PythonSslPython Requests

Python Problem Overview

import requests
data = {'foo':'bar'}
url = ''
r =, 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 =, 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.

Python Solutions

Solution 1 - Python


r =, data=data, verify='/path/to/public_key.pem')

Solution 2 - Python

With the verify parameter you can provide a custom certificate authority bundle

requests.get(url, verify=path_to_bundle_file)

From the docs:

> You can pass verify the path to a CA_BUNDLE file with certificates of > trusted CAs. This list of trusted CAs can also be specified through > the REQUESTS_CA_BUNDLE environment variable.

Solution 3 - Python

The easiest is to export the variable REQUESTS_CA_BUNDLE that points to your private certificate authority, or a specific certificate bundle. On the command line you can do that as follows:

export REQUESTS_CA_BUNDLE=/path/to/your/certificate.pem

If you have your certificate authority and you don't want to type the export each time you can add the REQUESTS_CA_BUNDLE to your ~/.bash_profile as follows:

echo "export REQUESTS_CA_BUNDLE=/path/to/your/certificate.pem" >> ~/.bash_profile ; source ~/.bash_profile

Solution 4 - Python

Case where multiple certificates are needed was solved as follows: Concatenate the multiple root pem files, myCert-A-Root.pem and myCert-B-Root.pem, to a file. Then set the requests REQUESTS_CA_BUNDLE var to that file in my ./.bash_profile.

$ cp myCert-A-Root.pem ca_roots.pem
$ cat myCert-B-Root.pem >> ca_roots.pem
$ echo "export REQUESTS_CA_BUNDLE=~/PATH_TO/CA_CHAIN/ca_roots.pem" >> ~/.bash_profile ; source ~/.bash_profile

Solution 5 - Python

Setting export SSL_CERT_FILE=/path/file.crt should do the job.

Solution 6 - Python

If you're behind a corporate network firewall like I was, ask your network admin where your corporate certificates are, then:

import os
os.environ["REQUESTS_CA_BUNDLE"] = 'path/to/corporate/cert.pem'
os.environ["SSL_CERT_FILE"] = 'path/to/corporate/cert.pem'

This fixed issues I had with requests and openssl.

Solution 7 - Python

All of the answers to this question point to the same path: get the PEM file, but they don't tell you how to get it from the website itself.

Getting the PEM file from the website itself is a valid option if you trust the site, such as on an internal corporate server. If you trust the site, why should you do this? You should do this because it helps protect yourself and others from inadvertently re-using your code on a site that isn't safe.

Here is how you can get the PEM file.

  1. Click on the lock next to the url. Click on the lock

  2. Navigate to where you can see the certificates and open the certificates. Navigate to view certificate

  3. Download the PEM CERT chain. Download the PEM chain

  4. Put the .PEM file somewhere you script can access it and try verify=r"path\to\pem_chain.pem" within your requests call.

r = requests.get(url, verify='\path\to\public_key.pem')

Solution 8 - Python

Incase anyone happens to land here (like I did) looking to add a CA (in my case Charles Proxy) for httplib2, it looks like you can append it to the cacerts.txt file included with the python package.

For example:

cat ~/Desktop/charles-ssl-proxying-certificate.pem >> /usr/local/google-cloud-sdk/lib/third_party/httplib2/cacerts.txt

The environment variables referenced in other solutions appear to be requests-specific and were not picked up by httplib2 in my testing.

Solution 9 - Python

You may try:

settings = s.merge_environment_settings(prepped.url, None, None, None, None)

You can read more here:

Solution 10 - Python

In a dev environment, using Poetry as virtual env provider on a Mac with Python 3.8 I used this answer as base and appended the content of my self-signed root certificate to the certifi cacert.pem file.

The steps in detail:

cd project_folder
poetry add requests
# or if you use something else, make sure certifi is among the dependencies
poetry shell
>>> import certifi
>>> certifi.where()
>>> exit()
cat /path/to/self-signed-root-cert.pem >> /path/to/the/certifi/cacert.pem

Solution 11 - Python

I know it is an old thread. However, I run into this issue recently. My python requests code does not accept the self-signed certificate but curl does. It turns out python requests are very strict on the self-signed certificate. It needs to be a root CA certificate. In other words,

> Basic Constraints: CA:TRUE

> Key Usage: Digital Signature, Non Repudiation, Key Encipherment, Certificate Sign


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew MoisenView Question on Stackoverflow
Solution 1 - PythonkrockView Answer on Stackoverflow
Solution 2 - PythonDr. Jan-Philip GehrckeView Answer on Stackoverflow
Solution 3 - PythonMike NView Answer on Stackoverflow
Solution 4 - PythonHalbert StoneView Answer on Stackoverflow
Solution 5 - PythongizzmoleView Answer on Stackoverflow
Solution 6 - PythonJordan MView Answer on Stackoverflow
Solution 7 - PythonFoggyView Answer on Stackoverflow
Solution 8 - PythonMat SchafferView Answer on Stackoverflow
Solution 9 - PythonganView Answer on Stackoverflow
Solution 10 - PythonAlexView Answer on Stackoverflow
Solution 11 - PythonYufengView Answer on Stackoverflow