How to Use Proxies with HTTPX (sync and async)
Configure HTTP and SOCKS5 proxies in Python HTTPX: the proxy argument, SOCKS support, AsyncClient with concurrent requests and error handling.
· 1 min read
HTTPX is an HTTP client for Python with both sync and async APIs. The examples were run with HTTPX 0.28 on Python 3.14.
Install
pip install httpx # HTTP proxies
pip install "httpx[socks]" # + SOCKS5 proxies
HTTP proxy
import httpx
with httpx.Client(proxy="http://LOGIN:PASSWORD@HOST:PORT", timeout=30) as client:
print(client.get("https://api.ipify.org?format=json").json())
Recent HTTPX versions use the proxy= argument (singular); the old proxies= argument was removed in 0.28.
SOCKS5 proxy
import httpx
with httpx.Client(proxy="socks5h://LOGIN:PASSWORD@HOST:PORT", timeout=30) as client:
print(client.get("https://api.ipify.org?format=json").json())
Use socks5h:// so that domain names are resolved by the proxy.
Async requests through one proxy
import asyncio
import httpx
PROXY = "socks5h://LOGIN:PASSWORD@HOST:PORT"
async def main():
async with httpx.AsyncClient(proxy=PROXY, timeout=30) as client:
responses = await asyncio.gather(*(client.get("https://api.ipify.org") for _ in range(3)))
print([r.status_code for r in responses])
asyncio.run(main())
All requests share one client and its connection pool.
Errors
Connection and authentication problems raise subclasses of httpx.HTTPError (for example httpx.ConnectError, httpx.ProxyError, httpx.ConnectTimeout). Wrap calls in try/except httpx.HTTPError and always set a timeout.
For the requests library, see How to Use SOCKS5 Proxies in Python.