Proxies with Python 'Requests' module
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Python requests supports HTTP and HTTPS proxies directly, which makes it a practical choice for debugging traffic, routing requests through corporate networks, or controlling outbound IP origin. The important parts are using the right proxy URL format, deciding whether configuration belongs per request or per session, and handling TLS and authentication intentionally.
Pass Proxy Settings Per Request
The basic API accepts a dictionary keyed by protocol.
Always add a timeout. Proxy hops introduce more ways for a request to stall.
Use a Session for Repeated Requests
When multiple requests share the same proxy configuration, a Session keeps the code cleaner and reuses underlying connections.
This is usually the better structure for scraping scripts, test utilities, or service clients.
Handle Proxy Authentication Safely
Authenticated proxies typically embed credentials in the proxy URL. If the username or password contains reserved characters, URL-encode them first.
Without encoding, characters such as @ or : can break the URL parser and produce misleading authentication errors.
Use Environment Variables When the Whole Process Shares the Proxy
For command-line programs or multi-module tools, environment variables can be simpler than passing a proxy dictionary around.
That pattern is common in CI environments and inside corporate networks.
Deal with TLS Verification Correctly
HTTPS through a proxy can fail because the proxy presents a certificate chain the client does not trust. The right fix is usually to provide the appropriate CA bundle, not to disable verification permanently.
If you disable TLS verification during debugging, keep that change local and temporary.
Add Retries When Proxies Are Unstable
Some proxy networks fail intermittently. A small retry policy is reasonable for idempotent requests.
That improves resilience without turning transient errors into endless hangs.
Verify That Traffic Is Really Going Through the Proxy
When debugging, hit an endpoint that reports the observed client IP or request headers. Compare the result with and without proxy configuration so you know the routing change is real.
Common Pitfalls
- Configuring only the
httpproxy entry and assuming HTTPS traffic will use it too. - Embedding raw credentials with special characters and getting a malformed proxy URL.
- Omitting timeouts, which makes proxy failures look like a frozen program.
- Disabling TLS verification instead of installing the correct certificate authority bundle.
- Forgetting
NO_PROXYfor internal hosts that should bypass the proxy entirely.
Summary
- Pass proxies as a protocol-to-URL mapping in
requests. - Use a
Sessionwhen many requests share the same proxy settings. - URL-encode proxy credentials before inserting them into the proxy URL.
- Prefer correct TLS trust configuration over disabling verification.
- Add timeouts and limited retries so proxy-related failures remain diagnosable.
Related reading
- proxy for distributed file share system in window
- Proxy Outbound/Egress Traffic Within Kubernetes
- Proxy setting not working with Spring WebClient
- Public IP of AWS Internet gateway
- psycopg2 insert multiple rows with one query
- Psycopg2 on Amazon Elastic Beanstalk
- Publish to RabbitMQ queue with HTTP API
- Publishing to the default rabbitmq exchange using the http api

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.