Created
August 21, 2026 12:42
-
-
Save quantumproxies/fb5026c7386003885e95524777c30b4f to your computer and use it in GitHub Desktop.
Rotating and sticky proxies with Python requests — the username carries the targeting https://quanticdata.io/blog/how-to-use-a-proxy-with-python-requests/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Rotating vs sticky proxies with Python requests, and how to prove which you got. | |
| pip install requests | |
| export QD_PROXY_USER=... QD_PROXY_PASS=... | |
| python3 qd_proxy_requests.py | |
| Targeting lives in the USERNAME — no custom headers, no extra API call: | |
| USER-country-us rotating US exit | |
| USER-country-de-city-berlin city-level | |
| USER-country-gb-session-a91f-sessTime-30 one IP, held 30 minutes | |
| No -session- means a NEW IP on every request. That is the default, and it is what | |
| you want for scraping. Add a session only when a flow needs continuity. | |
| https://quanticdata.io/residential-proxies/ | |
| Guide: https://quanticdata.io/blog/how-to-use-a-proxy-with-python-requests/ | |
| """ | |
| import os | |
| import secrets | |
| import requests | |
| GATEWAY = "pr.quanticdata.io:7777" | |
| USER = os.environ["QD_PROXY_USER"] | |
| PASS = os.environ["QD_PROXY_PASS"] | |
| CHECK = "https://ipinfo.io/json" | |
| def proxies(**bits) -> dict: | |
| parts = [USER] | |
| for key in ("country", "city", "session", "sessTime"): | |
| if bits.get(key): | |
| parts += [key, str(bits[key])] | |
| url = f"http://{'-'.join(parts)}:{PASS}@{GATEWAY}" | |
| return {"http": url, "https": url} | |
| # Rotating: a different exit each time. | |
| ips = [requests.get(CHECK, proxies=proxies(country="us"), timeout=30).json()["ip"] | |
| for _ in range(3)] | |
| print(f"rotating {len(set(ips))} distinct over 3: {ips}") | |
| # Sticky: one exit for the whole flow — required for carts, logins and queues. | |
| token = secrets.token_hex(3) | |
| with requests.Session() as s: | |
| s.proxies = proxies(country="de", session=token, sessTime=10) | |
| stuck = {s.get(CHECK, timeout=30).json()["ip"] for _ in range(3)} | |
| print(f"sticky {token} {stuck} <- one entry means it is working") | |
| # Common trap: leaving a session token in a "rotating" scraper. If your rotation | |
| # test returns the same IP every time, look at the username before the network. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment