01A request through the proxy
Build one proxy URL and pass it for both http and https. The https key covers https:// sites: requests opens a tunnel through the proxy, and the page stays encrypted end to end.
import requests
proxy = "http://USERNAME:PASSWORD@gw.fullstackproxy.com:47631"
proxies = {"http": proxy, "https": proxy}
r = requests.get("https://api.ipify.org?format=json", proxies=proxies, timeout=30)
print(r.json())Always set a timeout. Without one, a slow exit can hold a worker forever.
02Rotating: a new IP for each request
With no session in the username, each new connection gets a new IP. Plain requests.get calls open a new connection every time, so they rotate on their own:
import requests
proxy = "http://USERNAME-country-us:PASSWORD@gw.fullstackproxy.com:47631"
proxies = {"http": proxy, "https": proxy}
for page in range(1, 4):
r = requests.get(f"https://example.com/catalog?page={page}", proxies=proxies, timeout=30)
print(page, r.status_code)03Sticky: one IP for a whole flow
For a login, a checkout or paging through results, name a session. The same name keeps the same IP, for up to 24 hours with -lifetime-.
import uuid
import requests
session_id = uuid.uuid4().hex[:12] # any 1-25 letters, digits or _
user = f"USERNAME-country-us-session-{session_id}-lifetime-30"
proxy = f"http://{user}:PASSWORD@gw.fullstackproxy.com:47631"
with requests.Session() as s:
s.proxies = {"http": proxy, "https": proxy}
s.get("https://example.com/login", timeout=30)
r = s.get("https://example.com/account", timeout=30) # same IP as the login
print(r.status_code)04SOCKS5
requests needs the SOCKS extra: pip install "requests[socks]". Then use the socks5h:// scheme, so the hostname is looked up by the proxy rather than on your machine.
proxy = "socks5h://USERNAME:PASSWORD@gw.fullstackproxy.com:47631"
r = requests.get("https://api.ipify.org", proxies={"http": proxy, "https": proxy}, timeout=30)05Retries that change the IP
Some requests fail for reasons that have nothing to do with your code: the target is slow, or an exit drops off. On a rotating login a retry is a new connection, so it gets a new IP. urllib3 can do the retrying for you:
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
retry = Retry(total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504])
s = requests.Session()
s.mount("http://", HTTPAdapter(max_retries=retry))
s.mount("https://", HTTPAdapter(max_retries=retry))
s.proxies = {"http": "http://USERNAME:PASSWORD@gw.fullstackproxy.com:47631",
"https": "http://USERNAME:PASSWORD@gw.fullstackproxy.com:47631"}
print(s.get("https://example.com/", timeout=30).status_code)Do not retry a 402 or 407: those mean the balance is empty or the login is wrong, and the answer will not change. See error codes.
06Passwords with special characters
Inside a URL, characters like @, : and / have a meaning of their own. Encode the password before you put it in:
from urllib.parse import quote
password = quote("p@ss:word/1", safe="")
proxy = f"http://USERNAME:{password}@gw.fullstackproxy.com:47631"