Install
Python 3.9 or newer. Add the socks extra, httpx[socks], only if you connect over SOCKS5.
pip install httpxRotating residential
The module-level httpx.get builds a fresh client, and so a fresh connection, on every call. Each one opens its own tunnel through the residential gateway, so the three lines should print three different addresses.
import httpx
PROXY = "http://USER:[email protected]:8000"
for _ in range(3):
r = httpx.get("https://httpbin.org/ip", proxy=PROXY, timeout=30)
print(r.json()["origin"])A static datacenter or ISP IP
On a static datacenter IP, keep one client open so connections are reused. This is the async version: one AsyncClient fetching several URLs at once, with the connect timeout set apart from the rest. For an ISP order, use the host and port your dashboard lists for it.
import asyncio
import httpx
PROXY = "http://USER:PASS@IP:PORT"
URLS = ["https://httpbin.org/ip", "https://httpbin.org/headers"]
async def main():
timeout = httpx.Timeout(30, connect=10)
async with httpx.AsyncClient(proxy=PROXY, timeout=timeout) as client:
responses = await asyncio.gather(*(client.get(url) for url in URLS))
for r in responses:
print(r.status_code, r.text[:80])
asyncio.run(main())Keeping one identity
A Client or AsyncClient keeps a connection pool, and an HTTPS request through the proxy rides a tunnel that exits from one address. Reuse the client and requests to the same host share that tunnel and that IP, even on the rotating gateway, until the connection idles out. That suits a short multi-step flow and surprises you when you expected rotation. For a residential address that holds across new connections, use a sticky session; the session setting for your account is in the dashboard. python-telegram-bot runs on HTTPX, so the same pooling applies to a bot; Telegram bots with python-telegram-bot covers its two proxy settings.