Addon which activates PKCS12 certificates usage with HTTPX client.
Project description
httpx-pkcs12
Enhanced PKCS12 (PFX/P12) certificate support for the HTTPX Python client. This package allows you to easily use PKCS12 certificates with HTTPX for client certificate authentication.
Features
- Simple API for loading PKCS12 certificates
- Support for certificate validation
- Extract certificate information
- Works with both file paths and certificate data
- Accepts passwords as strings or bytes
- Proper cleanup of temporary certificate files
- Comprehensive type hints
Installation
pip install httpx-pkcs12
Or with uv:
uv add httpx-pkcs12
Usage
Basic Usage
import httpx
from httpx_pkcs12 import create_ssl_context
# Load certificate from file
context = create_ssl_context(
'path/to/your/cert.p12',
password='your-secret-password'
)
# With async client
async with httpx.AsyncClient(verify=context) as client:
response = await client.get('https://api.example.com')
# With sync client
with httpx.Client(verify=context) as client:
response = client.get('https://api.example.com')
# Or for a one-off request
response = httpx.get('https://api.example.com', verify=context)
Advanced Usage
from httpx_pkcs12 import create_ssl_context, get_certificate_info
# Load certificate directly from bytes
with open('path/to/cert.p12', 'rb') as f:
cert_data = f.read()
# Create context without validation (for expired certs)
context = create_ssl_context(
cert_data,
password='your-password',
validate=False
)
# Get certificate information
not_before, not_after, common_name, alt_names = get_certificate_info(
'path/to/cert.p12',
password='your-password'
)
print(f"Certificate: {common_name}")
print(f"Valid from: {not_before}")
print(f"Valid until: {not_after}")
print(f"Alternative names: {', '.join(alt_names)}")
API Reference
create_ssl_context(pkcs12_data, password=None, validate=True)
Creates an SSL context from PKCS12 data.
- pkcs12_data: The PKCS12 certificate data as bytes or a path to the file
- password: Password to decrypt the PKCS12 data (string or bytes)
- validate: Whether to validate certificate expiration
- Returns: An SSLContext object configured with the certificate
load_pkcs12_from_file(certificate_path, password=None, validate=True)
Convenience function to create an SSL context from a PKCS12 certificate file.
- certificate_path: Path to the PKCS12 certificate file
- password: Password to decrypt the PKCS12 data
- validate: Whether to validate certificate expiration
- Returns: An SSLContext object configured with the certificate
get_certificate_info(pkcs12_data, password=None)
Extracts and returns information about the certificate.
- pkcs12_data: The PKCS12 certificate data or file path
- password: Password to decrypt the PKCS12 data
- Returns: Tuple with (not_valid_before, not_valid_after, common_name, alt_names)
Error Handling
The package raises the following exceptions:
CertificateError: For certificate validation issuesValueError: For invalid input dataIOError: For file operation failures
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httpx_pkcs12-2.0.0.tar.gz.
File metadata
- Download URL: httpx_pkcs12-2.0.0.tar.gz
- Upload date:
- Size: 32.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ded85794b8a7bf8ca3fbc957a610d8a93907d31452f01e6ef0f485023877a857
|
|
| MD5 |
6fcc0691d92d04821e77f3c943427bfd
|
|
| BLAKE2b-256 |
6a117890b27a48ca2869fbce10a0487cea385642f3608c5798e68696f7ec042c
|
Provenance
The following attestation bundles were made for httpx_pkcs12-2.0.0.tar.gz:
Publisher:
publish.yml on theLastOfCats/httpx-pkcs12
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
httpx_pkcs12-2.0.0.tar.gz -
Subject digest:
ded85794b8a7bf8ca3fbc957a610d8a93907d31452f01e6ef0f485023877a857 - Sigstore transparency entry: 194041009
- Sigstore integration time:
-
Permalink:
theLastOfCats/httpx-pkcs12@06c94c966fbd447b055c905ccdf12e62b984dbdc -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/theLastOfCats
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@06c94c966fbd447b055c905ccdf12e62b984dbdc -
Trigger Event:
push
-
Statement type:
File details
Details for the file httpx_pkcs12-2.0.0-py3-none-any.whl.
File metadata
- Download URL: httpx_pkcs12-2.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b897fa1c98c839764ad126b661734c67cd6d87cff4b664ab6fa87fb635b7e93a
|
|
| MD5 |
e82061ad72bb532dd131302bc1ec5d19
|
|
| BLAKE2b-256 |
3de6c7dc08df35197f225e8db6ef0868239c345719c742bd738e15fb52f6466f
|
Provenance
The following attestation bundles were made for httpx_pkcs12-2.0.0-py3-none-any.whl:
Publisher:
publish.yml on theLastOfCats/httpx-pkcs12
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
httpx_pkcs12-2.0.0-py3-none-any.whl -
Subject digest:
b897fa1c98c839764ad126b661734c67cd6d87cff4b664ab6fa87fb635b7e93a - Sigstore transparency entry: 194041011
- Sigstore integration time:
-
Permalink:
theLastOfCats/httpx-pkcs12@06c94c966fbd447b055c905ccdf12e62b984dbdc -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/theLastOfCats
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@06c94c966fbd447b055c905ccdf12e62b984dbdc -
Trigger Event:
push
-
Statement type: