DEV Community

Cover image for Python Requests Proxy Setup: HTTP, HTTPS & SOCKS5 Guide

Python Requests Proxy Setup: HTTP, HTTPS & SOCKS5 Guide

Arpita Raval on November 30, 2026

Sooner or later, every Python developer who calls external websites at scale hits the same wall: rate limits, geo-restricted content, or an IP that...
Collapse
 
swapnil_jadhav profile image
Swapnil J •

Good breakdown, especially the sections on proxy rotation, retries and error handling.

One thing that becomes challenging at scale is not just configuring proxies but maintaining their reliability and managing the associated costs. A setup that works well for a few hundred requests can become quite difficult to maintain when you're processing thousands of requests regularly.

We encountered similar challenges while working with search engine data, which was one of the reasons we started building OrbitScraper.

I'm curious how other developers approach this. Do you prefer maintaining your own proxy infrastructure or using managed APIs when the request volume increases?

Collapse
 
arpitaraval profile image
Arpita Raval • • Edited

Thanks, glad the rotation, retries and error-handling sections were useful.

You're right that configuring a proxy is the easy part. At a few hundred requests, almost any setup works. At thousands a day, the work shifts to keeping the pool healthy (dead IPs, bans, uneven success rates) and keeping the cost per successful request under control. That's why I stressed retries and backoff in the guide, but they only treat the symptoms once the pool itself degrades.

On your question, I don't think it's an either/or. My rule of thumb:

  • Own proxy setup: Works for low to moderate volume, targets that are easy to scrape, or cases where you need full control over IPs, sessions and geo.
  • Managed APIs: Make more sense once the time spent on rotation, monitoring and replacing bad IPs costs more than the per-request fee, or when targets like search engines fight back hard.

Many teams end up with a hybrid: a managed layer for the difficult targets and their own proxies for everything else.

Search engine data is a good example of where the maintenance burden gets heavy, so I'm curious how OrbitScraper handles it. What request volume does it start making sense at, in your experience?

Collapse
 
swapnil_jadhav profile image
Swapnil J •

Thanks, Arpita. I agree with your point about the hybrid approach. Not every scraping use case necessarily requires a managed API.

With OrbitScraper, we're specifically focusing on search engine data, where maintaining proxies, handling CAPTCHAs, retries and blocked requests can quickly become a separate engineering task.

We handle those complexities on our side and provide structured search results through a credit-based API.

Regarding request volume, I don't think there's a fixed threshold. Even at a few hundred requests, a managed API can make sense if the target is difficult to scrape. At thousands of requests per day, infrastructure maintenance, engineering time and cost per successful request become much more important.

Our goal is to make search data collection practical and affordable for both independent developers and teams operating at scale.

I really like your point about measuring the cost per successful request rather than just the cost of proxies. That's an important distinction.

Thread Thread
 
arpitaraval profile image
Arpita Raval •

Thanks, that's a fair point. The threshold depends more on how hard the target is than on raw volume, and I'd rather judge it by cost per successful request than by the sticker price of proxies or API credits.

One thing I'd still check before choosing a managed API, especially for search data, is how it bills failures. Some charge per request regardless of outcome, others only charge for successful responses, and that changes the real cost a lot. Credit-based pricing is also easy to compare against a DIY setup if you track success rate, retries and engineering hours side by side.

Appreciate you sharing how OrbitScraper approaches it. Handling CAPTCHAs and blocks for search results is the kind of work most people underestimate until they're maintaining it.

Thread Thread
 
swapnil_jadhav profile image
Swapnil J •

Absolutely, Arpita. That's actually one of the things we considered when designing OrbitScraper's pricing model.

We only charge credits for successful search requests. Failed requests, internal retries and status checks don't consume credits.

I agree that comparing API pricing with a DIY setup should account for success rates, infrastructure maintenance and engineering time rather than just the advertised price per request.

Really appreciate the discussion and your perspective on this.

Collapse
 
jennifer-smith profile image
Jennifer Smith •

The socks5h:// vs socks5:// DNS distinction is something a lot of people miss, and it's a quiet way to leak your real location. I also liked the point about retrying only GET/HEAD, since blindly retrying POST can cause duplicate submissions. The curl -x trick for isolating proxy issues from Python issues is a handy debugging tip too. Bookmarking this as a reference. πŸ”–

Collapse
 
arpitaraval profile image
Arpita Raval •

Thanks so much! πŸ™Œ Glad those three points were useful. The socks5h:// one catches a lot of people out, because everything still works with plain socks5:// and the DNS leak only shows up when you check for it.

The GET/HEAD retry rule matters most in production. A retried POST that creates a duplicate order is much harder to clean up than a failed request.

If you run into a proxy edge case while using the guide, drop it here and I'll add it to the post. 🐍

Collapse
 
jennifer-smith profile image
Jennifer Smith •

That's a good point about socks5:// failing silently. Everything works, so there's nothing to tell you the DNS lookups are leaking.

I'll run an IP-echo check and a DNS leak test the next time I set up a new proxy. One edge case I'm curious about is socks5h:// combined with provider-side sticky sessions. Does the session parameter go in the username the same way as with HTTP proxies, or does it vary by provider? It might be worth a short note in the guide.

Looking forward to any updates! πŸš€

Thread Thread
 
arpitaraval profile image
Arpita Raval •

Exactly, that's what makes it nasty: the request succeeds, so nothing tells you DNS is leaking. An IP-echo check plus a DNS leak test is a solid habit for any new proxy.

On socks5h:// with sticky sessions: the session parameter usually goes in the username the same way as with HTTP proxies. socks5h only changes where DNS gets resolved (on the proxy side). Authentication is still username and password, so the credential format doesn't change with the protocol.

What does vary by provider:

Format: Some use user-session-abc123, others user_session-abc123 or user-sessid-abc123.
Endpoint: Some providers use a different port or hostname for SOCKS5 than for HTTP.
Session TTL: How long "sticky" actually lasts depends on the provider.

Here's a quick example:

proxies = {
    "http":  "socks5h://user-session-abc123:pass@proxy.example.com:1080",
    "https": "socks5h://user-session-abc123:pass@proxy.example.com:1080",
}
Enter fullscreen mode Exit fullscreen mode

Check your provider's docs for the exact syntax. You'll also need pip install requests[socks] for SOCKS support.

A short note on this in the guide is a good idea, and I'll add one. Thanks for the suggestion! πŸš€

Collapse
 
mayur-upadhyay profile image
Mayur Upadhyay •

Clear guide, and the explanation of the proxies dict keys (target scheme vs. proxy URL) is the part most tutorials get wrong. The socks5h:// DNS table is a nice touch too.

One edge case you could add to the environment variables section: when HTTP_PROXY / HTTPS_PROXY are set, they can silently override session.proxies. Proxies passed per request win over the environment, but proxies set on the Session don't. So if a container has HTTPS_PROXY exported, a Session configured with a different proxy can still route through the env one. It's a long-standing requests quirk (worth a quick check on your installed version). Passing proxies= on each call, or setting session.trust_env = False, avoids it. A one-line IP-echo check inside the actual Session catches it fast.

Two smaller things that might be worth a mention:

  • all:// as a key (e.g. {'all://': proxy}) is supported by httpx mounts, but plain requests only matches http/https, which trips people up when moving between the two.
  • With the retry adapter, ProxyError failures are retried by urllib3 before your own failover logic runs. In a rotation setup you may want total=0 on the adapter and handle failover yourself, so one dead proxy doesn't eat three attempts.
Collapse
 
arpitaraval profile image
Arpita Raval •

Thanks, glad the proxies dict explanation and the socks5h:// DNS table helped.

The env var point is a good catch. requests merges environment proxies into the request-level settings before it merges session.proxies, so with trust_env on, an exported HTTPS_PROXY can beat what you set on the Session. This bites a lot in containers and CI. I'll add a short note to the environment variables section with the two fixes you mentioned (proxies= per call, or session.trust_env = False) and an IP-echo check run through the actual Session.

The all:// difference is also worth a mention. In requests the catch-all key is plain all, and all:// as a bare key won't match. That's different from httpx mounts, which is where people get tripped up when switching libraries.

On retries, agreed. In a rotation setup, total=0 on the adapter and your own failover logic is cleaner, since otherwise urllib3 retries a dead proxy before your code ever sees the failure. I'll add that to the retry section.

Thanks for the detailed feedback!

Collapse
 
debugtodeploy profile image
Vinay Shah •

The proxies dictionary explanation is the part most guides get wrong. Calling out that the key is the target scheme and the value is how you talk to the proxy clears up why an https key can point to an http:// proxy URL. I also liked the socks5 vs socks5h table, since DNS leaks are easy to miss.

One question: For client-side rotation, have you tested how random.sample behaves once some proxies in the pool go bad? I'm wondering if you'd track failures and drop dead proxies for a cooldown period instead of picking them again on the next request.

Collapse
 
arpitaraval profile image
Arpita Raval •

Thanks, glad the proxies dictionary and the socks5h table were useful. Those were the two things I most wanted to get right.

Good question. random.sample in the guide is stateless: it picks k distinct proxies for that one call, so within a single fetch() you never retry the same dead proxy. But nothing carries over between calls, so a bad proxy can be picked again on the very next request. I kept the example simple, but you're right that it isn't enough for a real pool.

What I'd do is track consecutive failures per proxy and bench a proxy for a cooldown once it crosses a threshold, then let it back in and see if it recovers. Something like:

import time, random

COOLDOWN = 120   # seconds
MAX_FAILS = 3

state = {p: {"fails": 0, "until": 0} for p in PROXY_POOL}

def healthy():
    now = time.time()
    return [p for p, s in state.items() if s["until"] <= now]

def mark_fail(p):
    s = state[p]
    s["fails"] += 1
    if s["fails"] >= MAX_FAILS:
        s["until"] = time.time() + COOLDOWN
        s["fails"] = 0

def mark_ok(p):
    state[p]["fails"] = 0
Enter fullscreen mode Exit fullscreen mode

Then fetch() samples from healthy() instead of the full pool. One thing to watch is the failure type. ProxyError and ConnectTimeout point at the proxy, but a 403 or 429 usually means the target is blocking you, so I wouldn't bench the proxy for those. In production I'd also add a little jitter to the cooldown so the whole pool doesn't come back at once, and a lock if you're using threads.

I'll add a section on this to the guide. Thanks for the suggestion!

Collapse
 
taj_shai_694f78c2277b923d profile image
BoltGrab •

Helpful breakdown! One small gotcha worth keeping in mind with SOCKS5 in Python Requests is socks5:// vs socks5h://β€”using socks5h:// ensures DNS resolution happens on the proxy side rather than leaking local DNS queries.

Collapse
 
arpitaraval profile image
Arpita Raval •

Thanks, glad it helped! πŸ™Œ You're right about socks5h://, and it's the one I'd flag first for SOCKS5. That's why the guide uses it in the example and lists it in the pitfalls checklist. With plain socks5://, the local resolver still sees every hostname you visit, which can leak your location and partly defeats the point of using a proxy. Appreciate you calling it out for anyone skimming. πŸ™‚