← Docs / Proxies
Datacenter proxies with Python requests
The requests library sends traffic through a proxy with one argument. This page uses the datacenter tier, which gives you a US datacenter IP and rotates it for every new connection. Swap the password to use any other tier.
What you need
- Your proxy API key, from the Proxies page in the portal.
- Python 3 and
pip install requests.
Keep the key out of your source. Read it from an environment variable:
export PB_PROXY_KEY="YOUR_KEY"
Send a request through the proxy
import os
import requests
PROXY = f"http://{os.environ['PB_PROXY_KEY']}:[email protected]:3128"
proxies = {"http": PROXY, "https": PROXY}
r = requests.get("https://api.ipify.org", proxies=proxies, timeout=30)
print(r.text) # a US datacenter address, for example 203.0.113.7
The username is your API key. The password picks the tier. Always set a timeout: a request with none can hang forever.
Get a new IP each time
The exit address changes when a new connection opens, not on every HTTP request. How you call requests decides which you get.
# New connection on every call, so a new IP every time.
for _ in range(3):
print(requests.get("https://api.ipify.org", proxies=proxies, timeout=30).text)
# A Session reuses one connection, so the IP stays the same.
with requests.Session() as s:
s.proxies = proxies
for _ in range(3):
print(s.get("https://api.ipify.org", timeout=30).text)
# Session plus Connection: close, so a new IP every time again.
with requests.Session() as s:
s.proxies = proxies
s.headers["Connection"] = "close"
for _ in range(3):
print(s.get("https://api.ipify.org", timeout=30).text)
Use a plain call, or Connection: close, when you want every request to come from a different address. Use a Session when a site expects the same address across a few requests.
Keep the cost down
Billing counts the bytes on the wire. A new HTTPS connection costs about 4 KB of handshake before your first byte of data, and a reused connection costs a fraction of that. If you do not need a new IP per request, reuse a Session. See how usage is counted.
Use SOCKS5
Install the extra, then use the socks5h scheme on port 1080. The h makes the proxy resolve hostnames.
pip install "requests[socks]"
PROXY = f"socks5h://{os.environ['PB_PROXY_KEY']}:[email protected]:1080"
proxies = {"http": PROXY, "https": PROXY}
Errors
| What you see | What it means |
|---|---|
407 Proxy Authentication Required |
The API key is wrong or missing. |
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. |
400 Unknown tier "datacentre". Did you mean "datacenter"? |
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. |
More on errors: proxy errors and what they mean.
Related
Related
Still stuck? Open a ticket — tell us what was missing and the article gets fixed.