httpx Proxy Setup in Python: Sync, Async, and Rotation
Introduction
When working with web scraping and automation tasks at scale, proxies are essential for bypassing IP bans, load balancing requests, and maintaining anonymity. The httpx library is a modern HTTP client for Python that supports both synchronous and asynchronous operations, making it versatile for various use cases. This guide will walk you through setting up proxies in httpx, including synchronous and asynchronous clients, handling SOCKS5 proxies via the httpx-socks extension, and implementing a basic proxy rotation pattern.
Setting Up Synchronous Proxies with httpx
To start using proxies with httpx, you first need to install the library. You can do this using pip:
pip install httpx
Basic Proxy Usage
Here's how you can set up a synchronous client with a single proxy:
import httpx
proxies = {
"http://": "http://user:[email protected]:8080",
"https://": "http://user:[email protected]:8080"
}
with httpx.Client(proxies=proxies) as client:
response = client.get("http://example.org")
print(response.status_code)
Proxy Rotation
To implement a basic proxy rotation pattern, you can maintain a list of proxies and cycle through them:
import random
import time
proxy_list = [
"http://user:[email protected]:8080",
"http://user:[email protected]:8080",
"http://user:[email protected]:8080"
]
def rotate_proxy(proxies):
return random.choice(proxies)
with httpx.Client() as client:
while True:
proxy = rotate_proxy(proxy_list)
try:
response = client.get("http://example.org", proxies={"https://": proxy})
print(f"Response from {proxy}: {response.status_code}")
break # Exit the loop on success
except httpx.ProxyError as e:
print(f"Proxy error with {proxy}. Retrying... ({e})")
time.sleep(2)
Setting Up Asynchronous Proxies
For asynchronous operations, httpx provides an AsyncClient. Here's how to use it:
pip install httpx[async]
Basic Async Proxy Usage
Here's a simple example of using an asynchronous client with proxies:
import httpx
async def fetch_url(url):
proxies = {
"http://": "http://user:[email protected]:8080",
"https://": "http://user:[email protected]:8080"
}
async with httpx.AsyncClient(proxies=proxies) as client:
response = await client.get(url)
print(response.status_code)
import asyncio
asyncio.run(fetch_url("http://example.org"))
Async Proxy Rotation
For asynchronous proxy rotation, you can use a similar pattern to the synchronous one:
import random
import time
import asyncio
proxy_list = [
"http://user:[email protected]:8080",
"http://user:[email protected]:8080",
"http://user:[email protected]:8080"
]
def rotate_proxy(proxies):
return random.choice(proxies)
async def fetch_url(url):
while True:
proxy = rotate_proxy(proxy_list)
try:
async with httpx.AsyncClient(proxies={"https://": proxy}) as client:
response = await client.get(url)
print(f"Response from {proxy}: {response.status_code}")
break # Exit the loop on success
except httpx.ProxyError as e:
print(f"Proxy error with {proxy}. Retrying... ({e})")
await asyncio.sleep(2)
asyncio.run(fetch_url("http://example.org"))
Handling SOCKS5 Proxies
httpx does not support SOCKS5 proxies out of the box. However, you can use the httpx-socks extension to handle SOCKS5 proxies.
pip install httpx[socks]
Basic SOCKS5 Proxy Usage
Here's how you can set up a synchronous client with a SOCKS5 proxy:
import httpx
proxies = {
"http://": "socks5://user:[email protected]:1080",
"https://": "socks5://user:[email protected]:1080"
}
with httpx.Client(proxies=proxies) as client:
response = client.get("http://example.org")
print(response.status_code)
Conclusion
Using proxies with httpx is straightforward, whether you're working synchronously or asynchronously. The library's flexibility allows for both basic proxy usage and advanced patterns like rotation. Handling SOCKS5 proxies requires the httpx-socks extension, but it seamlessly integrates with httpx.
If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription: https://proxyforge.dev/#download.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →