Back to blog
Automation7 min read

Using Proxies with Python requests and httpx

Configure HTTP and SOCKS proxies in Python with requests and httpx — rotation, error handling, and production patterns.

Python remains the default language for data extraction. The requests library supports HTTP and HTTPS over HTTP proxies out of the box. For SOCKS, add requests[socks] or use httpx with transport plugins. This guide covers patterns that survive scale.

Basic setup with requests

Single HTTP proxy
import requests

proxies = {
    "http": "http://user:[email protected]:8080",
    "https": "http://user:[email protected]:8080",
}

resp = requests.get("https://api.example.com/data", proxies=proxies, timeout=30)
resp.raise_for_status()

Use the same proxy URL for both http and https keys — HTTPS still tunnels via CONNECT through the HTTP proxy endpoint.

Rotating proxies

Round-robin from a list
import itertools
import requests

pool = ["http://203.0.113.10:8080", "http://203.0.113.11:8080"]
cycle = itertools.cycle(pool)

def fetch(url):
    proxy = next(cycle)
    return requests.get(url, proxies={"http": proxy, "https": proxy}, timeout=20)

# Load lists from bulk feeds — see bulk proxy lists guide

Load pools from bulk proxy list files or plain-text API feeds. Filter dead endpoints with health checks before rotation.

Using httpx

httpx offers async support and clean proxy configuration for concurrent crawlers:

httpx async
import httpx

async with httpx.AsyncClient(proxy="http://203.0.113.10:8080") as client:
    r = await client.get("https://example.com")
    r.raise_for_status()

Error handling

  • Retry requests.exceptions.ProxyError with a new proxy from the pool.
  • Treat 407 as credential failure — do not infinite-retry the same proxy.
  • Log latency per proxy for pool quality metrics.

Production tips

Need proxies at scale?

proxies.st offers health-checked HTTP and SOCKS pools with dashboard access, API keys, and plain-text bulk feeds for pipelines.

Related guides