Created
March 8, 2020 09:00
-
-
Save jetersen/5efca1278e153aacc8413da71e77494c to your computer and use it in GitHub Desktop.
Python: add custom root ca to certifi store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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