A comprehensive HTTP client library that supports many features left out of other HTTP libraries.
npx @tessl/cli install tessl/pypi-httplib2@0.30.0A comprehensive HTTP client library for Python that supports many features left out of other HTTP libraries. httplib2 provides HTTP and HTTPS support with SSL verification, HTTP 1.1 Keep-Alive connections, multiple authentication methods (Digest, Basic, and WSSE), intelligent caching with Cache-Control support, all HTTP request methods, automatic redirects, compression handling, and lost update protection.
pip install httplib2import httplib2import httplib2
# Create an Http client with caching
h = httplib2.Http(".cache")
# Simple GET request
(resp, content) = h.request("http://example.org/", "GET")
print(resp.status)
print(content)
# POST request with data
body = "This is request body data"
headers = {'content-type': 'text/plain'}
(resp, content) = h.request("https://example.org/api", "POST",
body=body, headers=headers)
# Add authentication credentials
h.add_credentials('username', 'password')
(resp, content) = h.request("https://example.org/protected", "GET")httplib2 is built around several key components:
Core HTTP client functionality providing request/response handling, connection management, caching, and authentication. The main Http class serves as the primary interface for all HTTP operations.
class Http:
def __init__(self, cache=None, timeout=None, proxy_info=None,
ca_certs=None, disable_ssl_certificate_validation=False,
tls_maximum_version=None, tls_minimum_version=None):
"""HTTP client with caching and authentication support."""
def request(self, uri, method="GET", body=None, headers=None,
redirections=5, connection_type=None):
"""
Perform HTTP request.
Args:
uri (str): Absolute URI for the request
method (str): HTTP method (GET, POST, PUT, DELETE, etc.)
body (str): Request body data
headers (dict): HTTP headers
redirections (int): Maximum redirects to follow
connection_type: Connection class to use
Returns:
tuple: (Response, content) where Response is headers/status
and content is response body bytes
"""
def add_credentials(self, name, password, domain=""):
"""Add username/password for authentication."""
def add_certificate(self, key, cert, domain, password=None):
"""Add client certificate for authentication."""
def clear_credentials(self):
"""Remove all stored credentials."""Response objects provide access to HTTP status codes, headers, and response metadata. Response objects behave like dictionaries for header access while providing additional status information.
class Response(dict):
"""HTTP response containing status and headers."""
def __init__(self, info):
"""Initialize from HTTP response info."""
# Dictionary interface for headers
# Additional attributes: status, reason, versionMultiple authentication methods including Basic, Digest, WSSE, HMAC Digest, and Google Login authentication. Authentication is handled automatically once credentials are configured.
class Authentication:
"""Base authentication class."""
def request(self, method, request_uri, headers, content):
"""Modify request headers for authentication."""
def response(self, response, content):
"""Process authentication response."""
class BasicAuthentication(Authentication):
"""HTTP Basic authentication."""
class DigestAuthentication(Authentication):
"""HTTP Digest authentication."""
class WsseAuthentication(Authentication):
"""WSSE authentication."""File-based HTTP caching with Cache-Control header support, ETag validation, and Last-Modified handling. Caching improves performance by storing responses locally and respecting HTTP cache semantics.
class FileCache:
def __init__(self, cache, safe=None):
"""
File-based cache implementation.
Args:
cache (str): Cache directory path
safe (callable): Function to generate safe filenames
"""
def get(self, key):
"""Retrieve cached content."""
def set(self, key, value):
"""Store content in cache."""
def delete(self, key):
"""Remove cached content."""Comprehensive proxy server support including HTTP, HTTPS, and SOCKS proxies with authentication and bypass configuration. Proxy settings can be configured manually or detected from environment variables.
class ProxyInfo:
def __init__(self, proxy_type, proxy_host, proxy_port,
proxy_rdns=True, proxy_user=None, proxy_pass=None,
proxy_headers=None):
"""
Proxy configuration.
Args:
proxy_type: Type of proxy (socks.PROXY_TYPE_*)
proxy_host (str): Proxy hostname
proxy_port (int): Proxy port
proxy_rdns (bool): Use remote DNS resolution
proxy_user (str): Proxy username
proxy_pass (str): Proxy password
proxy_headers (dict): Additional headers for proxy requests
"""
def applies_to(self, hostname):
"""Check if proxy applies to hostname."""
def proxy_info_from_environment(method="http"):
"""Create ProxyInfo from environment variables."""
def proxy_info_from_url(url, method="http", noproxy=None):
"""Create ProxyInfo from URL."""Comprehensive exception hierarchy for handling HTTP errors, connection failures, authentication issues, and malformed responses. All exceptions derive from HttpLib2Error base class.
class HttpLib2Error(Exception):
"""Base exception for all httplib2 errors."""
class HttpLib2ErrorWithResponse(HttpLib2Error):
"""Exception that includes response data."""
def __init__(self, desc, response, content):
self.response = response
self.content = content
class RedirectLimit(HttpLib2ErrorWithResponse):
"""Too many redirects followed."""
class RedirectMissingLocation(HttpLib2ErrorWithResponse):
"""Redirect without Location header."""
class FailedToDecompressContent(HttpLib2ErrorWithResponse):
"""Content decompression failure."""URI parsing, normalization, IRI to URI conversion, certificate management, and other utility functions supporting the main HTTP client functionality.
def parse_uri(uri):
"""Parse URI into components."""
def urlnorm(uri):
"""Normalize URI format."""
def iri2uri(uri):
"""Convert IRI to URI."""
def has_timeout(timeout):
"""Check if timeout is set."""__version__ = "0.30.0"
debuglevel = 0 # HTTP debug level
RETRIES = 2 # Connection retry attempts
DEFAULT_MAX_REDIRECTS = 5 # Maximum redirects
SAFE_METHODS = ("GET", "HEAD", "OPTIONS", "TRACE")
REDIRECT_CODES = frozenset((300, 301, 302, 303, 307, 308))