Python HTTPX
Use NyxonProxies with HTTPX, sync and async: the proxy argument, clients, rotation with keep-alive off, sticky sessions, SOCKS5, mounts and errors.
HTTPX is a Python HTTP client with matching sync and async APIs. It takes a single proxy argument, on the top-level functions and on Client and AsyncClient.
Install
pip install httpxBasic request
import httpx
r = httpx.get(
"https://api.ipify.org?format=json",
proxy="http://USERNAME:[email protected]:8080",
timeout=30,
)
print(r.json())The proxy URL starts with http:// even for HTTPS sites. HTTPX opens a tunnel through the gateway and runs TLS to the site inside it.
Reuse a client
For more than a few requests, create one client and reuse it:
import httpx
with httpx.Client(proxy="http://USERNAME:[email protected]:8080", timeout=30) as client:
for _ in range(3):
print(client.get("https://api.ipify.org").text)A client keeps its connection to the gateway open, so these three requests share one IP.
A new IP on every request
Turn keep-alive off and every request opens a new connection, which gets a new IP on a rotating username:
import httpx
limits = httpx.Limits(max_keepalive_connections=0)
proxy = "http://USERNAME:[email protected]:8080"
with httpx.Client(proxy=proxy, limits=limits, timeout=30) as client:
for _ in range(3):
print(client.get("https://api.ipify.org").text)Async
AsyncClient takes the same argument. Requests that run in parallel each open their own connection, so they leave from different IPs:
import asyncio
import httpx
async def main():
async with httpx.AsyncClient(proxy="http://USERNAME:[email protected]:8080", timeout=30) as client:
responses = await asyncio.gather(*(client.get("https://api.ipify.org") for _ in range(5)))
for r in responses:
print(r.text)
asyncio.run(main())Country and sticky session
Put the targeting and the session in the username. Requests with the same session ID share one IP:
import secrets
import httpx
sid = secrets.token_hex(4)
proxy = f"http://USERNAME-country-us-session-{sid}-time-10:[email protected]:8080"
with httpx.Client(proxy=proxy, timeout=30) as client:
print(client.get("https://ipinfo.io/json").json())For parallel tasks that each need their own IP, give each task its own session ID. See Rotating and sticky sessions.
SOCKS5
SOCKS support needs an extra package:
pip install "httpx[socks]"import httpx
r = httpx.get(
"https://api.ipify.org",
proxy="socks5://USERNAME:[email protected]:1080",
timeout=30,
)
print(r.text)HTTPX sends the hostname to the gateway, so DNS is resolved on the proxy side. socks5h:// works the same way.
Route only some traffic
mounts maps URL patterns to transports. A value of None sends matching requests directly:
import httpx
mounts = {
"https://": httpx.HTTPTransport(proxy="http://USERNAME:[email protected]:8080"),
"all://127.0.0.1": None,
}
with httpx.Client(mounts=mounts, timeout=30) as client:
print(client.get("https://api.ipify.org").text)Passwords with special characters
Pass the credentials apart from the URL with httpx.Proxy, and no encoding is needed:
import httpx
proxy = httpx.Proxy("http://geo.gw.nyxon.sh:8080", auth=("USERNAME", "p@ss:w/rd#1"))
print(httpx.get("https://api.ipify.org", proxy=proxy, timeout=30).text)Environment variables
HTTPX reads HTTPS_PROXY, HTTP_PROXY, ALL_PROXY and NO_PROXY by default. A proxy= argument takes priority, and trust_env=False on the client ignores the environment. See Environment variables.
Errors
| Error | Cause |
|---|---|
httpx.ProxyError with 407 Proxy Authentication Required | Wrong username or password. |
A response with status 407 and no exception | The same mistake on a plain http:// site. Call r.raise_for_status() to catch it. |
ImportError mentioning socksio | Run pip install "httpx[socks]". |
httpx.InvalidURL | Special characters in the credentials. Use httpx.Proxy as above. |
httpx.ConnectError | Wrong host or port, a firewall, or https:// in front of the proxy URL. |
More in Error codes and Troubleshooting.
Last updated
Still stuck?
People answer the live chat around the clock. Send the proxy line you used (without the password) and the error you see.