Skip to main content

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 issues
  • ValueError: For invalid input data
  • IOError: 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

httpx_pkcs12-2.0.0.tar.gz (32.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

httpx_pkcs12-2.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

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

Hashes for httpx_pkcs12-2.0.0.tar.gz
Algorithm Hash digest
SHA256 ded85794b8a7bf8ca3fbc957a610d8a93907d31452f01e6ef0f485023877a857
MD5 6fcc0691d92d04821e77f3c943427bfd
BLAKE2b-256 6a117890b27a48ca2869fbce10a0487cea385642f3608c5798e68696f7ec042c

See more details on using hashes here.

Provenance

The following attestation bundles were made for httpx_pkcs12-2.0.0.tar.gz:

Publisher: publish.yml on theLastOfCats/httpx-pkcs12

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for httpx_pkcs12-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b897fa1c98c839764ad126b661734c67cd6d87cff4b664ab6fa87fb635b7e93a
MD5 e82061ad72bb532dd131302bc1ec5d19
BLAKE2b-256 3de6c7dc08df35197f225e8db6ef0868239c345719c742bd738e15fb52f6466f

See more details on using hashes here.

Provenance

The following attestation bundles were made for httpx_pkcs12-2.0.0-py3-none-any.whl:

Publisher: publish.yml on theLastOfCats/httpx-pkcs12

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page