Sending every request from one IP address is the quickest way to get rate limited. Rotating proxies spreads requests across many addresses so no single one draws attention. This guide covers the two rotation models, a working Python implementation with retries and health checks, how to react to 403, 429 and CAPTCHA responses, and how to keep the pool clean. Addresses are documentation examples (203.0.113.x, 198.51.100.x).
Per-request rotation vs sticky sessions
There are two models, and the right one depends on whether the target tracks state.
- Per-request rotation: each request leaves from a different IP. Good for stateless pages such as public listings or search results where no login or cart is involved.
- Sticky sessions: one IP is kept for a set period or number of requests. Required when the site ties a session cookie, login or multi-step flow to an address. Switching IP mid-session often logs you out or triggers a security check.
Many providers offer a single gateway address that rotates for you, with a session parameter in the username to request a sticky IP. Check your vendor's documentation, since the syntax differs. If you manage your own list, you implement rotation in code, as below.
A simple rotator in Python
itertools.cycle gives round-robin rotation. Wrap it with timeouts and a retry that moves to the next proxy on failure:
import itertools
import time
import requests
PROXIES = [
"http://203.0.113.10:8080",
"http://203.0.113.11:8080",
"http://198.51.100.7:3128",
]
pool = itertools.cycle(PROXIES)
def fetch(url, tries=4):
for attempt in range(tries):
proxy = next(pool)
try:
r = requests.get(
url,
proxies={"http": proxy, "https": proxy},
timeout=(5, 15),
)
if r.status_code == 200:
return r
if r.status_code in (403, 429):
time.sleep(2 ** attempt) # back off, then try another IP
continue
r.raise_for_status()
except requests.RequestException:
time.sleep(2 ** attempt)
raise RuntimeError("all attempts failed for " + url)
Use random.choice instead of cycle if you do not want a predictable order. For syntax details in other tools, see using a proxy with curl, Python and Node.js.
Sticky sessions with your own list
Bind a requests.Session to one proxy for the lifetime of a task:
def new_session():
proxy = next(pool)
s = requests.Session()
s.proxies.update({"http": proxy, "https": proxy})
return s
session = new_session() # reuse for login + following pages
Create a fresh session only when the old one fails or the task ends.
Handling 403, 429 and CAPTCHA
| Response | Usual meaning | Reasonable reaction |
|---|---|---|
| 429 Too Many Requests | You exceeded a rate limit | Slow down. Honor the Retry-After header if present, and add delay between requests, not just another IP. |
| 403 Forbidden | The IP, headers or behavior were rejected | Try another proxy, but also review headers and request pattern; a new IP will not fix a bad fingerprint. |
| CAPTCHA or challenge page | The site suspects automation | Reduce rate, use sticky sessions, consider a higher-reputation IP type. Do not assume the HTTP status is an error; challenge pages often return 200. |
| 407 | Proxy credentials rejected | Not a target block. See proxy error 407. |
| Timeout or connection reset | Dead or overloaded proxy | Retry on another proxy and count the failure. |
Rotation is not a substitute for politeness. Adding random delays, capping concurrency per site and caching pages you already fetched will reduce blocks more than adding IPs will. Detect challenge pages by content (for example a known marker string) and treat them as failures.
Health checks and removing dead proxies
Free and cheap proxies die constantly, and a pool that worked last week may be mostly dead today. Track failures per proxy and evict the ones that keep failing:
from collections import defaultdict
fails = defaultdict(int)
MAX_FAILS = 3
def mark_failure(proxy):
fails[proxy] += 1
if fails[proxy] >= MAX_FAILS and proxy in PROXIES:
PROXIES.remove(proxy)
def mark_success(proxy):
fails[proxy] = 0
If you use a plain list with cycle, rebuild the cycle after removing entries. Better still, validate the whole list before each run. Paste it into the proxy checker, keep the entries that respond and drop the rest, then feed the survivors to your scraper. Our checker tests HTTP, HTTPS and SOCKS5, so you also learn which scheme to use for each entry. See how to check if a proxy is working for what a pass or fail means.
Checks only tell you the proxy answered at that moment. Keep the in-code failure counting as well.
Ethics, robots.txt and terms of service
Technical ability is not permission. Before scraping a site:
- Read its
robots.txtand terms of service. Not honoring them can carry legal and contractual consequences, which vary by country and by what data you collect. - Prefer an official API or data export when one exists.
- Keep request rates low enough not to degrade the service for others.
- Avoid collecting personal data you have no lawful reason to hold.
- Identify your bot with an honest
User-Agentand contact address where appropriate.
When in doubt on legal questions, ask a qualified professional; this article is not legal advice.
Putting it together
A sensible workflow: validate the list, load the survivors, rotate per request for stateless pages or per session for stateful ones, back off on 429, count failures, and re-validate between runs. Log which proxy served each request together with the status code. When blocks rise, those logs show whether one subnet, one target or your own request rate is the cause, and that tells you whether to change proxies, slow down or change approach.
Common mistakes
- Rotating IPs in the middle of a login session.
- Retrying instantly in a tight loop, which turns a 429 into a ban.
- No timeouts, so one dead proxy stalls the whole job.
- Keeping dead proxies in the pool and wasting retries on them.
- Ignoring challenge pages that return HTTP 200 and saving them as real data.