Using Proxies in Python with httpx
httpx and Proxies
httpx is a modern HTTP library for Python that supports both synchronous and asynchronous modes of operation. A key advantage is its built-in support for HTTP/2 and SOCKS proxies without additional libraries.
Installation
pip install httpx
# For SOCKS support:
pip install httpx[socks]
# For HTTP/2:
pip install httpx[http2]
Synchronous Mode
HTTP Proxy
import httpx
proxy = "http://user:pass@proxy_ip:port"
response = httpx.get("https://httpbin.org/ip", proxy=proxy)
print(response.json())
Different Proxies for HTTP and HTTPS
proxies = {
"http://": "http://proxy1:port",
"https://": "http://proxy2:port",
}
with httpx.Client(proxy=proxies) as client:
response = client.get("https://httpbin.org/ip")
print(response.json())
SOCKS5 Proxy
import httpx
proxy = "socks5://user:pass@proxy_ip:port"
with httpx.Client(proxy=proxy) as client:
response = client.get("https://httpbin.org/ip")
print(response.json())
Asynchronous Mode
Basic Example
import httpx
import asyncio
async def fetch():
proxy = "http://user:pass@proxy_ip:port"
async with httpx.AsyncClient(proxy=proxy) as client:
response = await client.get("https://httpbin.org/ip")
print(response.json())
asyncio.run(fetch())
Parallel Requests
import httpx
import asyncio
async def fetch_url(client, url):
try:
response = await client.get(url, timeout=10)
return response.text
except Exception as e:
return None
async def main():
proxy = "http://user:pass@proxy_ip:port"
urls = [f"https://example.com/page/{i}" for i in range(50)]
async with httpx.AsyncClient(proxy=proxy) as client:
tasks = [fetch_url(client, url) for url in urls]
results = await asyncio.gather(*tasks)
success = sum(1 for r in results if r)
print(f"Success: {success}/{len(urls)}")
asyncio.run(main())
HTTP/2 via Proxy
httpx is one of the few Python libraries with HTTP/2 support:
import httpx
proxy = "http://user:pass@proxy_ip:port"
with httpx.Client(proxy=proxy, http2=True) as client:
response = client.get("https://httpbin.org/ip")
print(f"HTTP version: {response.http_version}")
print(response.json())
HTTP/2 via proxy works through the CONNECT method — the proxy creates a TCP tunnel through which an HTTP/2 connection to the server is established.
Proxy Rotation
import httpx
import random
PROXIES = [
"http://user:pass@proxy1:port",
"http://user:pass@proxy2:port",
"http://user:pass@proxy3:port",
]
def get_random_proxy():
return random.choice(PROXIES)
# Synchronous rotation
for i in range(10):
proxy = get_random_proxy()
with httpx.Client(proxy=proxy) as client:
resp = client.get("https://httpbin.org/ip")
print(resp.json())
Asynchronous Rotation
import httpx
import asyncio
import random
PROXIES = [
"http://user:pass@proxy1:port",
"http://user:pass@proxy2:port",
"http://user:pass@proxy3:port",
]
async def fetch_with_rotation(url):
proxy = random.choice(PROXIES)
async with httpx.AsyncClient(proxy=proxy) as client:
try:
resp = await client.get(url, timeout=10)
return resp.text
except Exception:
return None
async def main():
urls = ["https://example.com"] * 20
tasks = [fetch_with_rotation(url) for url in urls]
results = await asyncio.gather(*tasks)
print(f"Success: {sum(1 for r in results if r)}")
asyncio.run(main())
Client Configuration
Timeout
timeout = httpx.Timeout(
connect=5.0, # connection to proxy and server
read=10.0, # reading the response
write=5.0, # sending the request
pool=5.0 # waiting in the connection pool
)
client = httpx.Client(proxy=proxy, timeout=timeout)
Headers and Cookies
headers = {
"User-Agent": "Mozilla/5.0 ...",
"Accept-Language": "en-US,en;q=0.9",
}
client = httpx.Client(
proxy=proxy,
headers=headers,
follow_redirects=True,
max_redirects=10
)
Connection Pool
limits = httpx.Limits(
max_connections=100, # total connections
max_keepalive_connections=20 # keep-alive connections
)
client = httpx.AsyncClient(proxy=proxy, limits=limits)
Error Handling
import httpx
async def safe_fetch(client, url):
try:
response = await client.get(url)
response.raise_for_status()
return response.text
except httpx.ProxyError as e:
print(f"Proxy error: {e}")
except httpx.ConnectTimeout:
print("Connection timeout")
except httpx.ReadTimeout:
print("Read timeout")
except httpx.HTTPStatusError as e:
print(f"HTTP {e.response.status_code}")
except httpx.RequestError as e:
print(f"Request error: {e}")
return None
httpx vs requests vs aiohttp
| Parameter | httpx | requests | aiohttp |
|---|---|---|---|
| Sync | Yes | Yes | No |
| Async | Yes | No | Yes |
| HTTP/2 | Yes | No | No |
| SOCKS | Yes (built-in) | Via requests-socks | Via aiohttp-socks |
| Proxy in URL | Yes | Yes | Yes |
| API | requests-compatible | Standard | Custom |
| Performance | High | Medium | High |
Migration from requests
httpx is designed as a drop-in replacement for requests. Migration is minimal:
# requests
import requests
resp = requests.get(url, proxies={"https": proxy})
# httpx
import httpx
resp = httpx.get(url, proxy=proxy)
The main difference: in httpx, the proxy parameter (singular) is used instead of proxies.
Conclusion
httpx is the best choice for new Python projects requiring proxy functionality. Its built-in support for HTTP/2, SOCKS5, and both synchronous and asynchronous modes makes it a versatile tool. The requests-compatible API simplifies migration for existing projects.