← Docs / Proxies
Datacenter proxies with Python httpx
httpx takes the proxy as one argument. This page uses the datacenter tier: a US datacenter IP that changes for every new connection.
What you need
- Your proxy API key, from the Proxies page in the portal.
pip install httpx(version 0.26 or later, which uses theproxyargument).
export PB_PROXY_KEY="YOUR_KEY"
Send a request through the proxy
import os
import httpx
PROXY = f"http://{os.environ['PB_PROXY_KEY']}:[email protected]:3128"
r = httpx.get("https://api.ipify.org", proxy=PROXY, timeout=30)
print(r.text) # a US datacenter address
One IP, or a new one each time
A Client keeps its connection open, so all its requests share one address. A bare httpx.get() opens a new connection every time.
# New IP each call: bare calls open a new connection.
for _ in range(3):
print(httpx.get("https://api.ipify.org", proxy=PROXY, timeout=30).text)
# Same IP for all calls: one Client reuses its connection.
with httpx.Client(proxy=PROXY, timeout=30) as client:
for _ in range(3):
print(client.get("https://api.ipify.org").text)
Async
For a new IP on every request with an AsyncClient, stop it keeping connections alive:
import asyncio
async def main():
limits = httpx.Limits(max_keepalive_connections=0)
async with httpx.AsyncClient(proxy=PROXY, timeout=30, limits=limits) as client:
for _ in range(3):
print((await client.get("https://api.ipify.org")).text)
asyncio.run(main())
Leave keep-alive on when a steady address or a smaller bill matters more. See how usage is counted.
Errors
| What you see | What it means |
|---|---|
407 Proxy Authentication Required |
The API key is wrong or missing. |
400 with Unknown tier "datacentre" |
The tier word is a close misspelling of a real tier. Fix the spelling. A password that is not close to any tier, such as x, uses your account's default tier. |
400 Invalid proxy password settings |
The password asked for something this tier does not do: a country other than US, a session or a TTL. |
A site returns 403 |
The site is refusing datacenter addresses. Try a residential tier such as standard. |
httpx raises httpx.ProxyError when the proxy refuses the tunnel.
Related
Related
Still stuck? Open a ticket — tell us what was missing and the article gets fixed.