Skip to content

Instantly share code, notes, and snippets.

@jetersen
Created March 8, 2020 09:00
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jetersen/5efca1278e153aacc8413da71e77494c to your computer and use it in GitHub Desktop.
Save jetersen/5efca1278e153aacc8413da71e77494c to your computer and use it in GitHub Desktop.
Python: add custom root ca to certifi store
import requests
import certifi
import sys
try:
requests.get('https://any-website-protected-by-your-custom-root-ca')
print('Certificate already added to the certifi store')
sys.exit(0)
except requests.exceptions.SSLError as err:
print('SSL Error. Adding custom certs to Certifi store...')
customca = requests.get('http://place-to-download-your-root-ca-pem.file').content
cafile = certifi.where()
with open(cafile, 'ab') as outfile:
outfile.write(b'\n')
outfile.write(customca)
try:
requests.get('https://any-website-protected-by-your-custom-root-ca-to-validate-the-certificate')
print('Successfully added certificate to the certifi store')
sys.exit(0)
except requests.exceptions.SSLError as err:
print('Failed to add certificate to the certifi store')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment