Python package for providing Mozilla's CA Bundle for SSL certificate validation
npx @tessl/cli install tessl/pypi-certifi@2024.12.0Python package for providing Mozilla's carefully curated collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts. It has been extracted from the Requests project and serves as a reliable and highly portable root of trust to Python deployments.
pip install certifiimport certifiFor accessing specific functions:
from certifi import where, contentsimport certifi
# Get the path to the CA bundle file
ca_bundle_path = certifi.where()
print(ca_bundle_path)
# Output: /path/to/site-packages/certifi/cacert.pem
# Get the contents of the CA bundle
ca_bundle_contents = certifi.contents()
print(ca_bundle_contents[:100]) # First 100 characters
# Output: -----BEGIN CERTIFICATE-----
# MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw...Command line usage:
# Get the path to the CA bundle
python -m certifi
# Output: /path/to/site-packages/certifi/cacert.pem
# Get the contents of the CA bundle
python -m certifi --contents
# Output: -----BEGIN CERTIFICATE-----
# MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw...Returns the filesystem path to the bundled CA certificate file (cacert.pem).
def where() -> str:
"""
Get the filesystem path to the bundled CA certificate file.
Returns:
str: The absolute path to the cacert.pem file containing Mozilla's
CA certificate bundle. The file is guaranteed to exist and be readable.
"""The where() function uses version-specific implementations to handle different Python environments:
importlib.resources.as_file and filesimportlib.resources.path and manages resource cleanupReturns the complete contents of the CA certificate bundle as a string.
def contents() -> str:
"""
Get the contents of the bundled CA certificate file.
Returns:
str: The complete contents of the cacert.pem file as ASCII text,
containing Mozilla's CA certificate bundle in PEM format.
Each certificate begins with "-----BEGIN CERTIFICATE-----"
and ends with "-----END CERTIFICATE-----".
"""The contents() function also uses version-specific implementations:
importlib.resources.files with read_textimportlib.resources.read_textwhere() function__version__ = "2024.12.14"
__all__ = ["contents", "where"]The package can be executed as a module to access certificate bundle information from the command line:
# Default behavior: print path to CA bundle
python -m certifi
# Print contents of CA bundle
python -m certifi -c
python -m certifi --contentsimport ssl
import certifi
# Create SSL context with certifi's CA bundle
context = ssl.create_default_context(cafile=certifi.where())
# Use in urllib
import urllib.request
urllib.request.urlopen('https://example.com', context=context)import requests
import certifi
# Use certifi's CA bundle explicitly
response = requests.get('https://example.com', verify=certifi.where())import certifi
# Verify the CA bundle exists and contains certificates
ca_path = certifi.where()
ca_contents = certifi.contents()
print(f"CA bundle location: {ca_path}")
print(f"Number of certificates: {ca_contents.count('-----BEGIN CERTIFICATE-----')}")The package is designed to be robust and typically does not raise exceptions under normal usage. However, potential issues include:
where() may still return a path but contents() could fail when readingatexit to manage resource contexts properlyimportlib.resources APIs are not available