Guides
Ways to Use Proxies with Python Requests
The Python requests library makes adding proxies straightforward, but doing it well, with rotation, authentication, and error handling, is what keeps scrapers reliable.
The requests library is the go-to choice for HTTP work in Python, and pairing it with proxies is one of the most common needs in scraping and data collection. A proxy routes your request through another address, which spreads traffic and helps avoid the blocks that hit repeated calls from a single IP.
Getting started is simple, but a production-ready setup involves more than one line of configuration. Authentication, rotation, timeouts, and graceful failure handling all matter once you move beyond a handful of requests.
This guide walks through the practical patterns and ties them back to choosing the proxy type and plan that suit your workload.
The Basic Proxy Setup
At its simplest, requests accepts a dictionary mapping protocols to proxy URLs. You pass it via the proxies argument, and the library routes the call accordingly.
import requests
proxies = {
"http": "http://USER:PASS@host:port",
"https": "http://USER:PASS@host:port"
}
r = requests.get("https://httpbin.org/ip", proxies=proxies, timeout=10)
print(r.json())That small snippet covers the essentials. Note that even HTTPS targets are often routed through an http:// proxy URL, which trips up newcomers. Always set a timeout so a stalled proxy does not hang your script indefinitely while it waits for a response that never arrives.
Handling Authentication Cleanly
Most paid proxies require credentials. The inline form, embedding the username and password in the URL, works but can clutter your code and leak secrets if logged. A tidier approach keeps credentials in environment variables and builds the URL at runtime.
Some providers also support IP-allowlisting, where you register your server's address and skip credentials entirely. That can simplify scripts considerably and avoid storing secrets in code. Whichever method you use, never commit raw credentials to version control. Read your provider's documentation, because the supported authentication methods can depend on the plan, and the cleaner option may already be available without changing tools.
Rotating Proxies Across Requests
Sending every request through one proxy reintroduces the single-IP problem you were trying to solve. Rotation spreads calls across a pool so no single address shoulders all the traffic.
A simple pattern keeps a list of proxies and cycles through them, while many providers offer a rotating endpoint that hands you a fresh IP automatically on each call. The latter is often easier than managing a list yourself.
- Manual list rotation gives you fine control but more code to maintain.
- Provider rotation endpoints reduce code and handle pool management for you.
Choose based on how much control you need versus how much you want the provider to handle, and confirm the rotation behavior matches your task.
Sticky Sessions When You Need Them
Rotation is not always desirable. Some workflows, logging in, navigating a multi-step form, maintaining a cart, need the same IP across several requests, or the site treats each call as a new, suspicious visitor.
For these, a sticky session holds one address for a set duration. With requests, you can pin a specific proxy for a sequence of calls, often using a requests.Session object to persist cookies alongside the IP. Many providers expose sticky-session endpoints designed for exactly this. Decide per task whether you need spread (rotation) or continuity (sticky), since mixing them up causes broken logins or wasted IPs depending on which way you err.
Robust Error Handling and Retries
Proxies fail sometimes, and a script that crashes on the first error is fragile. Wrap requests in try/except blocks, set sensible timeouts, and retry failed calls with a different proxy rather than giving up.
for attempt in range(3):
try:
r = requests.get(url, proxies=pick_proxy(), timeout=10)
r.raise_for_status()
break
except requests.RequestException:
continueBacking off between retries and rotating to a new IP on failure dramatically improves reliability. Logging which proxies fail repeatedly helps you spot dead addresses and avoid wasting attempts on them, turning a brittle script into one that survives the inevitable hiccups of real-world networks.
Choosing the Right Proxy Type
The proxy type shapes both cost and success rate. Datacenter proxies are fast and economical, ideal for tolerant sites and high-volume tasks where blending in is not critical. Residential proxies map to real consumer connections and handle stricter targets better.
Mobile proxies go further still on the most defensive sites. Picking the right tier avoids two failure modes: overpaying for realism you do not need, or under-provisioning and getting blocked. Our proxy types guide compares them in detail. Match the type to your specific targets, and remember that performance can depend on the plan and locations you choose rather than the type label alone.
Respecting Rate Limits and Target Sites
Proxies let you send more requests, but that is not a license to hammer a site. Aggressive scraping can harm the target's performance and quickly triggers defenses no matter how many IPs you have.
Build in delays, respect any published rate limits, and check the site's terms and robots guidance. Spreading requests over time, in addition to spreading them across IPs, keeps your activity sustainable and reduces the chance of escalating blocks. Responsible pacing also means your proxy pool lasts longer, because addresses that behave reasonably are far less likely to be flagged than ones firing requests at machine speed without pause.
Testing and Scaling Your Setup
Before a large run, verify your proxy setup against a simple endpoint that echoes your IP, confirming requests actually route through the proxy and the address changes when you rotate. Catching a misconfiguration here saves a wasted run later.
Start small, measure the success rate, and scale only once the pattern holds. If failures cluster on certain targets, you may need a different proxy type rather than more of the same. Our buying guide covers sizing a plan to real demand. Growing gradually keeps costs proportionate to results and prevents you from buying capacity that your scripts cannot yet use effectively.
What to compare before buying
Before you order, weigh these points so the proxies you pick match your real workload and budget:
- Supported authentication methods: credential-based versus IP-allowlisting
- Availability of rotating endpoints versus managing your own proxy list
- Sticky-session support for multi-step flows like logins and carts
- Proxy type matched to your targets: datacenter, residential, or mobile
- Location coverage relevant to the sites you scrape
- How the provider documents setup with Python and requests specifically
- Pricing model and how it scales with your request volume
- Ability to test against a small allocation before committing
Frequently asked questions
Pass a dictionary mapping protocols to proxy URLs via the proxies argument, for example proxies={'http': 'http://user:pass@host:port', 'https': '...'}. Always set a timeout so a slow proxy cannot hang your script indefinitely.
The proxy scheme describes how you connect to the proxy, not the destination. Many providers route HTTPS traffic through an http:// proxy endpoint, which surprises newcomers but is normal and works as expected.
Either cycle through a list you maintain or use a provider rotating endpoint that returns a fresh IP per call. The endpoint approach needs less code; a manual list gives finer control over which addresses you use.
Use a sticky session when several requests must share one IP, such as logging in or completing a multi-step flow. A requests.Session plus a pinned proxy keeps cookies and the address consistent across those calls.
Wrap calls in try/except, set timeouts, and retry with a different proxy on failure. Logging repeatedly failing addresses lets you skip dead IPs, turning a fragile script into one that tolerates normal network errors.
Datacenter proxies suit tolerant, high-volume targets; residential proxies handle stricter sites; mobile proxies suit the most defensive ones. Match the type to your targets to avoid overpaying or getting blocked unnecessarily.
Request an endpoint that echoes your IP and check the returned address matches the proxy, then confirm it changes when you rotate. This quick test catches misconfiguration before you start a large, costly run.
Related pages worth comparing
Have a comparison question about how to use proxies with python requests? Email info@comparebestproxy.com.