requests-pkcs12

1.27 · active · verified Sun Apr 12

The requests-pkcs12 library extends the popular Python `requests` library to add native support for client-side PKCS#12 (often .p12 or .pfx) certificates. It provides a clean implementation by creating a custom `TransportAdapter` and `SSLContext`, avoiding monkey patching or the use of unencrypted temporary files. Currently at version 1.27, it serves as a robust transitional solution until `requests` incorporates direct PKCS#12 support. The project appears to be actively maintained, with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform both one-off and session-based HTTP requests using a PKCS#12 client certificate. It requires a `.p12` file and its corresponding password. For security, these values are retrieved from environment variables, or fall back to placeholders for demonstration. Remember to replace `PKCS12_FILENAME`, `PKCS12_PASSWORD`, and `TARGET_URL` with your actual certificate path, password, and the secure endpoint you wish to access.

import os
from requests import Session
from requests_pkcs12 import Pkcs12Adapter, get

# --- Example 1: Simple one-off request ---
# Requires a client certificate file (e.g., clientcert.p12) and its password.
# Ensure 'pkcs12_filename' points to a valid .p12 file
# and 'pkcs12_password' is correct for testing.

PKCS12_FILENAME = os.environ.get('PKCS12_FILENAME', 'clientcert.p12') # Placeholder
PKCS12_PASSWORD = os.environ.get('PKCS12_PASSWORD', 'your_pkcs12_password') # Placeholder
TARGET_URL = os.environ.get('TARGET_URL', 'https://example.com/secure_endpoint') # Placeholder

try:
    print(f"\nAttempting one-off GET to {TARGET_URL}...")
    r = get(
        TARGET_URL,
        pkcs12_filename=PKCS12_FILENAME,
        pkcs12_password=PKCS12_PASSWORD,
        verify=True # Always verify server certificates in production!
    )
    r.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
    print(f"One-off GET successful! Status: {r.status_code}")
    # print(r.text)
except Exception as e:
    print(f"One-off GET failed: {e}")

# --- Example 2: Using with a requests Session (recommended for multiple requests) ---

try:
    print(f"\nAttempting session-based GET to {TARGET_URL}...")
    with Session() as s:
        s.mount(
            'https://',
            Pkcs12Adapter(
                pkcs12_filename=PKCS12_FILENAME,
                pkcs12_password=PKCS12_PASSWORD
            )
        )
        # The 'verify' parameter can be set on the session or per request.
        # It is crucial for verifying the server's identity.
        r_session = s.get(TARGET_URL, verify=True)
        r_session.raise_for_status()
        print(f"Session-based GET successful! Status: {r_session.status_code}")
        # print(r_session.text)
except Exception as e:
    print(f"Session-based GET failed: {e}")

# Note: For actual testing, replace 'clientcert.p12' and 'your_pkcs12_password'
# with a real PKCS#12 file path and its password. You might need a dummy
# server that requires client certificate authentication for full testing.

view raw JSON →