v1.10.82-f67ee7d
Skip to main content
← Back to Hex Proxies

Python httpx Proxy Integration

Integrate Hex Proxies with Python httpx for modern async and sync HTTP requests with built-in proxy rotation and geo targeting.

Why httpx for Proxy-Based Workflows

httpx has become the go-to Python HTTP client for teams that need both synchronous and asynchronous request capabilities in a single library. Unlike the older requests library, httpx natively supports HTTP/2, connection pooling across proxied connections, and async context managers that make it straightforward to build high-throughput scraping pipelines. When paired with residential proxies, httpx excels at maintaining long-lived sessions while rotating exit IPs transparently.

The library's proxy handling is notably more flexible than alternatives. You can configure proxies at the client level for all requests, override them per-request, or use httpx's event hook system to dynamically select proxies based on the target domain. This granularity is critical when you need sticky sessions for checkout flows on one domain while simultaneously rotating IPs for catalog scraping on another.

Complete Configuration Example

import httpx

proxy_url = f"http://{os.environ['PROXY_USER']}:{os.environ['PROXY_PASS']}@gate.hexproxies.com:8080"

transport = httpx.HTTPTransport( proxy=proxy_url, retries=3, verify=True, )

with httpx.Client( transport=transport, timeout=httpx.Timeout(connect=10.0, read=30.0, write=10.0, pool=5.0), limits=httpx.Limits(max_connections=20, max_keepalive_connections=10), headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}, follow_redirects=True, ) as client: response = client.get("https://example.com") print(response.status_code, len(response.text))

# Async variant for high-throughput workloads # async with httpx.AsyncClient(transport=transport) as aclient: # tasks = [aclient.get(url) for url in url_list] # results = await asyncio.gather(*tasks) ```

httpx-Specific Proxy Features

httpx separates transport configuration from client configuration, which means you can swap proxy transports without rebuilding your client. The `HTTPTransport` class accepts a `retries` parameter that automatically retries on connection failures at the transport level, before your application-level retry logic ever fires. This two-layer retry approach dramatically improves success rates through residential proxies where occasional connection resets are expected.

Common Pitfalls with httpx

The most frequent mistake is passing proxy configuration using the legacy `proxies` dictionary format. While httpx accepted this in earlier versions, modern httpx (0.24+) requires proxy configuration through the `transport` parameter or the `proxy` shorthand on the client constructor. Using the old format silently ignores your proxy settings, sending requests directly. Another subtle issue: httpx's default connection pool limits are conservative. When running concurrent requests through a proxy gateway, you should explicitly set `max_connections` to match your concurrency target, otherwise httpx will queue requests and artificially throttle throughput.

Advanced: Event Hooks for Dynamic Proxy Selection

httpx supports request and response event hooks. You can use a request hook to log which proxy IP was used, measure latency per-region, or dynamically modify headers based on the target site. Response hooks let you detect soft blocks (200 status but CAPTCHA content) and trigger automatic IP rotation before the next request fires.

Performance Tuning

Enable HTTP/2 by passing `http2=True` to the client constructor. HTTP/2 multiplexing through a single proxy connection reduces handshake overhead significantly when making many requests to the same origin. Combine this with `max_keepalive_connections` tuned to your proxy plan's concurrency limit for optimal throughput.

Integration Steps

1

Install httpx and configure credentials

Run pip install httpx and store your Hex Proxies username and password in environment variables. Never hardcode credentials in source files.

2

Create a transport with proxy and retry settings

Instantiate httpx.HTTPTransport with your proxy URL and a retry count of 2-3 to handle transient proxy connection resets gracefully.

3

Build a client with connection limits and timeouts

Configure httpx.Client with granular timeouts (connect, read, write, pool) and connection limits matching your proxy plan concurrency.

4

Test with a geo-verification endpoint and monitor headers

Hit an IP-check API to confirm traffic routes through the proxy. Inspect response headers for proxy-injected metadata indicating the exit node region.

Operational Tips

Keep sessions stable for workflows that depend on consistent identity. For high-volume collection, rotate IPs and reduce concurrency if you see timeouts or 403 responses.

  • Prefer sticky sessions for multi-step flows (auth, checkout, forms).
  • Rotate per request for scale and broad coverage.
  • Use timeouts and retries to handle transient failures.

Frequently Asked Questions

How do I use httpx with async and proxies simultaneously?

Use httpx.AsyncClient with an httpx.AsyncHTTPTransport that has the proxy parameter set. The async client supports the same proxy configuration as the sync client, and you can use asyncio.gather to run concurrent proxied requests efficiently.

Why are my httpx proxy settings being ignored?

If you are on httpx 0.24 or later, the proxies dictionary parameter was removed. You must configure proxies through the transport parameter or the proxy shorthand on the client constructor. Check your httpx version with pip show httpx.

Can httpx maintain sticky sessions through a proxy?

Yes. Use the session_id parameter in your Hex Proxies username (e.g., user-session-abc123) to pin an exit IP. httpx will reuse the same connection through the proxy gateway, and the gateway will route to the same residential IP for the session duration.

Ready to Integrate?

Start using residential proxies with Python httpx today.