CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-httplib2

A comprehensive HTTP client library that supports many features left out of other HTTP libraries.

Pending
Overview
Eval results
Files

proxy-support.mddocs/

Proxy Support

Comprehensive proxy server support including HTTP, HTTPS, and SOCKS proxies with authentication and bypass configuration. Proxy settings can be configured manually or detected automatically from environment variables.

Capabilities

ProxyInfo Class

Configuration class for proxy server settings including authentication and bypass rules.

class ProxyInfo:
    """Proxy configuration and management."""
    
    def __init__(self, proxy_type, proxy_host, proxy_port,
                 proxy_rdns=True, proxy_user=None, proxy_pass=None,
                 proxy_headers=None):
        """
        Initialize proxy configuration.
        
        Args:
            proxy_type: Proxy type (socks.PROXY_TYPE_HTTP, socks.PROXY_TYPE_SOCKS4, 
                       socks.PROXY_TYPE_SOCKS5)
            proxy_host (str): Proxy server hostname or IP
            proxy_port (int): Proxy server port number
            proxy_rdns (bool): Use remote DNS resolution (default True)
            proxy_user (str): Username for proxy authentication (optional)
            proxy_pass (str): Password for proxy authentication (optional)  
            proxy_headers (dict): Additional headers for proxy requests (optional)
        """
    
    def astuple(self):
        """
        Return proxy configuration as tuple.
        
        Returns:
            tuple: (proxy_type, proxy_host, proxy_port, proxy_rdns, 
                   proxy_user, proxy_pass, proxy_headers)
        """
    
    def isgood(self):
        """
        Check if proxy configuration is valid.
        
        Returns:
            bool: True if proxy can be used
        """
    
    def applies_to(self, hostname):
        """
        Check if proxy should be used for hostname.
        
        Args:
            hostname (str): Target hostname
            
        Returns:
            bool: True if proxy applies to this hostname
        """
    
    def bypass_host(self, hostname):
        """
        Check if hostname should bypass proxy.
        
        Args:
            hostname (str): Target hostname
            
        Returns:
            bool: True if hostname should bypass proxy
        """

Proxy Utility Functions

Functions for creating proxy configurations from environment variables and URLs.

def proxy_info_from_environment(method="http"):
    """
    Create ProxyInfo from environment variables.
    
    Checks environment variables in order:
    - <method>_proxy (e.g., http_proxy, https_proxy)
    - <METHOD>_PROXY (uppercase version)
    - no_proxy/NO_PROXY for bypass list
    
    Args:
        method (str): Protocol method ("http" or "https")
        
    Returns:
        ProxyInfo: Proxy configuration, or None if no proxy configured
    """

def proxy_info_from_url(url, method="http", noproxy=None):
    """
    Create ProxyInfo from proxy URL.
    
    Args:
        url (str): Proxy URL (e.g., "http://proxy.example.com:8080")
        method (str): Protocol method
        noproxy (str): Comma-separated list of hosts to bypass
        
    Returns:
        ProxyInfo: Proxy configuration
    """

Proxy Types

httplib2 supports multiple proxy types through the PySocks library:

# Import proxy types from socks module
import socks

# Available proxy types:
socks.PROXY_TYPE_HTTP     # HTTP proxy (most common)
socks.PROXY_TYPE_SOCKS4   # SOCKS4 proxy  
socks.PROXY_TYPE_SOCKS5   # SOCKS5 proxy (supports authentication)

Usage Examples

Basic HTTP Proxy

import httplib2
import socks

# Configure HTTP proxy
proxy_info = httplib2.ProxyInfo(
    socks.PROXY_TYPE_HTTP,
    'proxy.example.com',
    8080
)

h = httplib2.Http(proxy_info=proxy_info)
(resp, content) = h.request("http://example.org/")

Proxy with Authentication

import httplib2
import socks

# HTTP proxy with username/password
proxy_info = httplib2.ProxyInfo(
    socks.PROXY_TYPE_HTTP,
    'proxy.example.com', 
    8080,
    proxy_user='proxyuser',
    proxy_pass='proxypass'
)

h = httplib2.Http(proxy_info=proxy_info)
(resp, content) = h.request("https://secure.example.com/")

SOCKS5 Proxy

import httplib2
import socks

# SOCKS5 proxy configuration
proxy_info = httplib2.ProxyInfo(
    socks.PROXY_TYPE_SOCKS5,
    'socks.example.com',
    1080,
    proxy_user='sockuser',
    proxy_pass='sockpass'
)

h = httplib2.Http(proxy_info=proxy_info)
(resp, content) = h.request("http://example.org/")

Environment Variable Configuration

import httplib2
import os

# Set environment variables
os.environ['http_proxy'] = 'http://proxy.example.com:8080'
os.environ['https_proxy'] = 'http://proxy.example.com:8080'
os.environ['no_proxy'] = 'localhost,127.0.0.1,.example.com'

# Create Http client that automatically uses environment proxy settings
h = httplib2.Http(proxy_info=httplib2.proxy_info_from_environment)

# Requests will use proxy based on environment variables
(resp, content) = h.request("http://external.com/")      # Uses proxy
(resp, content) = h.request("http://internal.example.com/")  # Bypasses proxy

Proxy Auto-Detection

import httplib2

# Function-based proxy configuration
def get_proxy_info(method):
    """Dynamic proxy selection based on method."""
    if method == "https":
        return httplib2.ProxyInfo(
            httplib2.socks.PROXY_TYPE_HTTP,
            'https-proxy.example.com', 
            8443
        )
    else:
        return httplib2.ProxyInfo(
            httplib2.socks.PROXY_TYPE_HTTP,
            'http-proxy.example.com',
            8080
        )

h = httplib2.Http(proxy_info=get_proxy_info)
(resp, content) = h.request("http://example.org/")   # Uses HTTP proxy
(resp, content) = h.request("https://example.org/")  # Uses HTTPS proxy

Proxy from URL

import httplib2

# Create proxy info from URL string
proxy_url = "http://user:pass@proxy.example.com:8080"
proxy_info = httplib2.proxy_info_from_url(proxy_url)

h = httplib2.Http(proxy_info=proxy_info)
(resp, content) = h.request("http://example.org/")

Proxy Bypass

Configure hosts that should bypass the proxy:

Environment Variable Bypass

import os
import httplib2

# Configure proxy bypass via environment
os.environ['http_proxy'] = 'http://proxy.example.com:8080'
os.environ['no_proxy'] = 'localhost,127.0.0.1,*.local,.internal.com'

h = httplib2.Http(proxy_info=httplib2.proxy_info_from_environment)

# These requests bypass the proxy:
(resp, content) = h.request("http://localhost/")
(resp, content) = h.request("http://server.local/")
(resp, content) = h.request("http://api.internal.com/")

# This request uses the proxy:
(resp, content) = h.request("http://external.com/")

Programmatic Bypass

import httplib2
from httplib2 import AllHosts

# Create proxy with bypass configuration
class CustomProxyInfo(httplib2.ProxyInfo):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.bypass_hosts = ['.internal.com', 'localhost']
    
    def bypass_host(self, hostname):
        hostname = '.' + hostname.lstrip('.')
        for bypass in self.bypass_hosts:
            if hostname.endswith(bypass):
                return True
        return False

proxy_info = CustomProxyInfo(
    httplib2.socks.PROXY_TYPE_HTTP,
    'proxy.example.com',
    8080
)

h = httplib2.Http(proxy_info=proxy_info)

Proxy Headers

Add custom headers to proxy requests:

import httplib2
import socks

# Proxy with custom headers
proxy_headers = {
    'User-Agent': 'MyApp/1.0',
    'X-Forwarded-For': '192.168.1.100'
}

proxy_info = httplib2.ProxyInfo(
    socks.PROXY_TYPE_HTTP,
    'proxy.example.com',
    8080,
    proxy_headers=proxy_headers
)

h = httplib2.Http(proxy_info=proxy_info)
(resp, content) = h.request("http://example.org/")

HTTPS Through Proxy

HTTPS requests through HTTP proxies use the CONNECT method:

import httplib2
import socks

# HTTP proxy for HTTPS requests
proxy_info = httplib2.ProxyInfo(
    socks.PROXY_TYPE_HTTP,
    'proxy.example.com',
    8080,
    proxy_user='user',
    proxy_pass='pass'
)

h = httplib2.Http(proxy_info=proxy_info)

# HTTPS request tunneled through HTTP proxy
(resp, content) = h.request("https://secure.example.com/api")

Error Handling

import httplib2

try:
    proxy_info = httplib2.ProxyInfo(
        httplib2.socks.PROXY_TYPE_HTTP,
        'nonexistent-proxy.example.com',
        8080
    )
    
    h = httplib2.Http(proxy_info=proxy_info)
    (resp, content) = h.request("http://example.org/")
    
except httplib2.ProxiesUnavailableError:
    print("Proxy support not available (PySocks not installed)")
except httplib2.ServerNotFoundError:
    print("Proxy server not found")
except Exception as e:
    print(f"Proxy error: {e}")

Performance Considerations

Connection Pooling

  • Proxy connections are pooled separately from direct connections
  • Connection key includes proxy information
  • Reuses proxy connections when possible

DNS Resolution

  • proxy_rdns=True: DNS resolution through proxy (default)
  • proxy_rdns=False: Local DNS resolution
  • SOCKS proxies support remote DNS resolution

Authentication Caching

  • Proxy authentication credentials cached per proxy
  • Reduces authentication overhead
  • Automatically retries with credentials on 407 Proxy Authentication Required

Proxy Configuration Patterns

Corporate Environment

import httplib2
import os

# Typical corporate proxy setup
os.environ.update({
    'http_proxy': 'http://corporate-proxy.company.com:8080',
    'https_proxy': 'http://corporate-proxy.company.com:8080',
    'no_proxy': 'localhost,127.0.0.1,*.company.com,10.*,192.168.*'
})

h = httplib2.Http(proxy_info=httplib2.proxy_info_from_environment)

Development Environment

import httplib2

# Development proxy for debugging
proxy_info = httplib2.ProxyInfo(
    httplib2.socks.PROXY_TYPE_HTTP,
    '127.0.0.1',  # Local proxy like Charles or Fiddler
    8888
)

h = httplib2.Http(proxy_info=proxy_info)

Load Balancing Proxies

import httplib2
import random

# Multiple proxy servers for load balancing
proxy_servers = [
    ('proxy1.example.com', 8080),
    ('proxy2.example.com', 8080),
    ('proxy3.example.com', 8080)
]

def get_random_proxy(method):
    host, port = random.choice(proxy_servers)
    return httplib2.ProxyInfo(
        httplib2.socks.PROXY_TYPE_HTTP,
        host, port
    )

h = httplib2.Http(proxy_info=get_random_proxy)

Install with Tessl CLI

npx tessl i tessl/pypi-httplib2

docs

authentication.md

caching.md

error-handling.md

http-client.md

index.md

proxy-support.md

response-handling.md

utilities.md

tile.json