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.1.tar.gz (57.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.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file httpx_pkcs12-2.0.1.tar.gz.

File metadata

  • Download URL: httpx_pkcs12-2.0.1.tar.gz
  • Upload date:
  • Size: 57.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for httpx_pkcs12-2.0.1.tar.gz
Algorithm Hash digest
SHA256 9f30b38848f166c05e27e25966733d1afd0e953daf99cfe26b029f10209cf467
MD5 0807d56ee961c4ccd3a9042c0dca8e9f
BLAKE2b-256 d235d9a804989c432c1c6030d9ae9e127b72fccc560e54b9ccfbe7748d8b19a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for httpx_pkcs12-2.0.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: httpx_pkcs12-2.0.1-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.13.12

File hashes

Hashes for httpx_pkcs12-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89b0612e5fb7fc6fcab2cfa05d78e8ac319c6584cc0bc7c10ef79cff6a8ff062
MD5 580c4371d5a69b284fc28bb766c97072
BLAKE2b-256 55b4e825badca5109aab4195b480431d749dc051e75d1b5efb820643d82d0885

See more details on using hashes here.

Provenance

The following attestation bundles were made for httpx_pkcs12-2.0.1-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