Skip to main content

modern-httplib2

A 100% compatible, zero-code-change drop-in replacement for Python's httplib2 library, powered under the hood by httpcore and h2.

Why modern-httplib2?

Legacy httplib2 is deeply embedded across the Python ecosystem (notably via google-api-python-client, google-auth-httplib2, and multi-threaded tools like GAM), but its original transport layer is built directly on Python's low-level http.client with single-socket state per Http() instance.

modern-httplib2 keeps the exact httplib2 public API, module layout, exception hierarchy, RFC 2616 caching, authentication schemes (Basic, Digest), and ProxyInfo semantics while replacing the underlying transport engine with httpcore + h2:

  • Python Package & PyPI Name: Published as modern-httplib2, importing as modern_httplib2.
  • Transparent Interceptor / Auto-Patching: Providing import modern_httplib2.auto or modern_httplib2.patch() which mounts modern_httplib2 into sys.modules["httplib2"]. Existing dependencies like google-api-python-client and google-auth-httplib2 automatically and transparently use modern_httplib2 without any code changes to Google's libraries.
  • Direct Import Support: Use directly via import modern_httplib2 or import modern_httplib2 as httplib2.
  • Lean httpcore + h2 Transport: Uses httpcore's synchronous connection pool and h11/h2 protocol state machines directly with minimal runtime dependencies.
  • True Multi-Threaded HTTP/2 Multiplexing: Multiple threads sharing a single Http() instance multiplex concurrent requests over a single TCP+TLS HTTP/2 connection (up to 100 concurrent streams per connection), even on cold start.
  • Thread-Safe Connection Pooling: Unlike upstream httplib2.Http() (which is notorious for thread-safety crashes when shared across threads), modern-httplib2 uses thread-local active request dispatch backed by a thread-safe per-origin httpcore.ConnectionPool (pool_maxsize=16 by default).
  • Transparent HTTP/2 over TLS (ALPN h2): Automatically negotiates HTTP/2 on HTTPS connections when the server advertises h2 via TLS ALPN, with seamless fallback to HTTP/1.1.
  • Python 3.11+ Native: Built exclusively for supported Python versions (3.11+), free of legacy Python 2 / early Python 3 compatibility shims.
  • 100% Upstream Test Suite Compatibility: Passes all 526+ upstream httplib2 unit and integration tests.

Installation

Install from PyPI:

pip install modern-httplib2

Usage

1. Transparent Interception in Existing Projects (e.g. GAM)

To automatically redirect all import httplib2 calls across your application and its dependencies (like google-api-python-client and google-auth-httplib2) to modern_httplib2, import modern_httplib2.auto at your application entrypoint:

try:
    import modern_httplib2.auto
except ImportError:
    pass

# All downstream imports now transparently use modern_httplib2!
import google_auth_httplib2
import googleapiclient.discovery
import httplib2

http = httplib2.Http()

2. Direct Import Usage

import modern_httplib2 as httplib2

http = httplib2.Http(".cache")
resp, content = http.request("https://httpbin.org/get", "GET")
print(resp.status, resp.version)  # e.g. 200, 20 (HTTP/2) or 11 (HTTP/1.1)

2. Thread-Safe Concurrent Requests

You can safely share a single httplib2.Http instance across multiple threads:

from concurrent.futures import ThreadPoolExecutor
import httplib2

http = httplib2.Http(pool_maxsize=16)

def fetch(url):
    resp, body = http.request(url)
    return resp.status, len(body)

with ThreadPoolExecutor(max_workers=8) as pool:
    results = list(pool.map(fetch, ["https://example.com"] * 32))

3. Controlling HTTP/2 Negotiation & Corporate Firewall Downgrade

HTTP/2 over TLS is enabled by default (enable_http2=True). For environments where corporate firewalls, deep packet inspection (DPI) middleboxes, or legacy proxies reject, reset, or misbehave with HTTP/2 traffic, modern-httplib2 provides multiple ways to force HTTP/1.1:

Automatic Firewall Fallback (Enabled by Default)

By default (auto_downgrade_http2=True), if an HTTP/2 connection fails due to a network or protocol reset (e.g. proxy RST on HTTP/2 preface, stream errors, or closed connections), modern-httplib2 will:

  1. Automatically downgrade the connection to HTTP/1.1.
  2. Remember that host for subsequent requests in the session.
  3. Transparently retry the request without raising an exception to the caller.

Global / Application-Wide Toggle

Disable HTTP/2 globally in your application (for example, when a user sets force_http11 = true in their configuration file):

import httplib2

# Any of these can be called at startup:
httplib2.force_http11()
# or:
httplib2.ENABLE_HTTP2 = False
# or:
httplib2.set_enable_http2(False)

Environment Variable (Zero Code Change)

Set the environment variable in your shell or configuration:

export MODERN_HTTPLIB2_ENABLE_HTTP2=0
# or
export HTTPLIB2_ENABLE_HTTP2=0

Per-Instance Control

import httplib2

http_v1 = httplib2.Http(enable_http2=False)
# or dynamically on an existing instance:
http_v1.force_http11()

resp, content = http_v1.request("https://example.com")
assert resp.version == 11

4. Using with google-auth-httplib2

import google.auth
import google_auth_httplib2
import httplib2

credentials, project = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
authed_http = google_auth_httplib2.AuthorizedHttp(credentials, http=httplib2.Http())
resp, content = authed_http.request("https://www.googleapis.com/oauth2/v1/ tokeninfo")

Running Tests

.venv/bin/pytest

Maintenance & Compatibility

modern-httplib2 is maintained as a stable, low-churn drop-in transport layer:

  • Upstream Alignment: Automated weekly checks monitor httplib2/httplib2 for protocol or security updates.
  • Continuous Canary Testing: Weekly scheduled CI runs test against both minimum supported dependency versions and the latest PyPI releases of httpcore, h2, pyparsing, certifi, google-auth, google-auth-httplib2, google-api-python-client, pre-release CPython, and pip-audit.

Metadata

Release files for modern-httplib2 1.32.1.post2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modern-httplib2 1.32.1.post2
File Size Uploaded Provenance Actions
modern_httplib2-1.32.1.post2.tar.gz 72.8 kB With provenance Details for modern_httplib2-1.32.1.post2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for modern-httplib2 1.32.1.post2
File Interpreter ABI Platform Actions
modern_httplib2-1.32.1.post2-py3-none-any.whl Python 3 none any Details for modern_httplib2-1.32.1.post2-py3-none-any.whl

Total release size: 112.2 kB

Release files / modern_httplib2-1.32.1.post2.tar.gz

Download URL modern_httplib2-1.32.1.post2.tar.gz
Size 72.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5916dd6ff3a638ba9eecf8a3b8979f76b48af3178a6001c5217c2c57b8026455
BLAKE2b-256 checksum
How to use checksums
f11e9f7118ed552c65153408f5db62112d46ddeab3896c542e65faa3c485fa7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / modern_httplib2-1.32.1.post2-py3-none-any.whl

Download URL modern_httplib2-1.32.1.post2-py3-none-any.whl
Size 39.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97d6da98569304f6be3bf8292b495230ac2e30e9ec2aa28ca9607c2e528fe90a
BLAKE2b-256 checksum
How to use checksums
55d3da8467851c0b19d3c4067c00054774db6671509bf176c6bad93836d8ea09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for modern-httplib2 1.32.1.post2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for modern_httplib2-1.32.1.post2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page