HTTP library with thread-safe connection pooling, file post support, user friendly interface, and more.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Sophisticated connection pool management for high-performance HTTP clients. Pool managers automatically handle connection pooling across different hosts and protocols, providing optimal performance for applications making many HTTP requests.
The main interface for managing connection pools across multiple hosts. Automatically creates and manages HTTP/HTTPS connection pools based on the target URL, providing connection reuse and thread-safe operation.
class PoolManager:
def __init__(self, num_pools=10, headers=None, **connection_pool_kw):
"""
HTTP connection pool manager.
Parameters:
- num_pools: Number of connection pools to cache (default: 10)
- headers: Headers to add to all requests
- **connection_pool_kw: Additional keyword arguments for connection pools
"""
def request(self, method: str, url: str, **kw) -> BaseHTTPResponse:
"""
Make a request using appropriate connection pool.
Parameters:
- method: HTTP method (GET, POST, etc.)
- url: Complete URL including scheme and host
- **kw: Additional request arguments
Returns:
BaseHTTPResponse: Response object
"""
def urlopen(self, method: str, url: str, **kw) -> BaseHTTPResponse:
"""
Same as request() method - provided for compatibility.
"""
def clear(self):
"""
Empty the pooled connections and clear the cache.
"""Specialized pool manager for making requests through HTTP or HTTPS proxies. Handles proxy authentication and tunnel setup automatically.
class ProxyManager(PoolManager):
def __init__(self, proxy_url, num_pools=10, headers=None, **connection_pool_kw):
"""
HTTP proxy connection pool manager.
Parameters:
- proxy_url: URL of the proxy server (http://proxy.example.com:8080)
- num_pools: Number of connection pools to cache
- headers: Headers to add to all requests
- **connection_pool_kw: Additional keyword arguments for connection pools
"""def proxy_from_url(url: str, **kw) -> ProxyManager:
"""
Create a ProxyManager from a proxy URL.
Parameters:
- url: Proxy URL (e.g., 'http://proxy.example.com:8080')
- **kw: Additional keyword arguments for ProxyManager
Returns:
ProxyManager: Configured proxy manager
"""import urllib3
# Create a pool manager
http = urllib3.PoolManager()
# Make requests - pools are created automatically
resp1 = http.request('GET', 'https://httpbin.org/get')
resp2 = http.request('GET', 'https://jsonplaceholder.typicode.com/posts/1')
resp3 = http.request('POST', 'https://httpbin.org/post', fields={'key': 'value'})
print(f"Response 1: {resp1.status}")
print(f"Response 2: {resp2.status}")
print(f"Response 3: {resp3.status}")import urllib3
# Pool manager with default headers
http = urllib3.PoolManager(
headers={'User-Agent': 'MyApp/1.0'},
timeout=urllib3.Timeout(connect=1.0, read=2.0),
retries=urllib3.Retry(total=3)
)
resp = http.request('GET', 'https://httpbin.org/headers')import urllib3
import ssl
# Create custom SSL context
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
# Pool manager with custom SSL context
http = urllib3.PoolManager(ssl_context=ctx)
resp = http.request('GET', 'https://self-signed.badssl.com/')import urllib3
# Create proxy manager
proxy = urllib3.ProxyManager('http://proxy.example.com:8080')
# All requests go through the proxy
resp = proxy.request('GET', 'https://httpbin.org/get')
# Or use proxy_from_url utility
proxy = urllib3.proxy_from_url('http://proxy.example.com:8080')
resp = proxy.request('GET', 'https://httpbin.org/get')import urllib3
# Proxy with authentication
proxy = urllib3.ProxyManager(
'http://username:password@proxy.example.com:8080'
)
resp = proxy.request('GET', 'https://httpbin.org/get')import urllib3
# Pool manager with custom connection pool settings
http = urllib3.PoolManager(
num_pools=20, # Cache up to 20 different host pools
maxsize=10, # Max 10 connections per pool
block=True, # Block when pool is full
timeout=urllib3.Timeout(connect=1.0, read=5.0),
retries=urllib3.Retry(total=3, backoff_factor=0.3)
)
# Make many requests efficiently
for i in range(100):
resp = http.request('GET', f'https://httpbin.org/get?id={i}')
print(f"Request {i}: {resp.status}")import urllib3
http = urllib3.PoolManager()
# Use the pool manager
resp = http.request('GET', 'https://httpbin.org/get')
# Clear all pooled connections when done
http.clear()Install with Tessl CLI
npx tessl i tessl/pypi-urllib3