CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-urllib3

HTTP library with thread-safe connection pooling, file post support, user friendly interface, and more.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

pool-management.mddocs/

Pool Management

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.

Capabilities

PoolManager

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.
        """

ProxyManager

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
        """

Proxy Creation Utility

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
    """

Usage Examples

Basic PoolManager Usage

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}")

Custom Headers and Configuration

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')

HTTPS with Custom SSL Context

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/')

Proxy Usage

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')

Proxy with Authentication

import urllib3

# Proxy with authentication
proxy = urllib3.ProxyManager(
    'http://username:password@proxy.example.com:8080'
)

resp = proxy.request('GET', 'https://httpbin.org/get')

Connection Pool Configuration

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}")

Managing Pool Lifecycle

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()

Performance Benefits

  • Connection Reuse: Automatically reuses connections to the same host
  • Thread Safety: Safe to use from multiple threads
  • Automatic Pool Management: Creates and destroys pools based on usage
  • Protocol Support: Handles both HTTP and HTTPS automatically
  • Keep-Alive: Maintains persistent connections when supported by server

Install with Tessl CLI

npx tessl i tessl/pypi-urllib3

docs

configuration.md

connection-pools.md

exceptions.md

index.md

pool-management.md

response-handling.md

simple-requests.md

utilities.md

tile.json