How to Set Up Proxy in Python Requests: Session, Env, SOCKS5

Start with the answer: Direct answer
Python Requests accepts proxy settings per call through the proxies parameter, across several calls through Session.proxies, or from HTTP_PROXY and HTTPS_PROXY environment variables. SOCKS5 requires requests[socks]; use socks5h when hostname resolution should occur through the assigned proxy endpoint. Always set timeouts, catch Requests exceptions, verify through a permitted endpoint, and keep real credentials outside the repository.
Choose the narrowest configuration scope that fits the program. A per-request dictionary is easiest to review, a Session is useful for repeated calls, and environment variables are best reserved for controlled runtimes where their wider effect is understood.
Prepare fields and load secrets
| Pattern | When to use it |
|---|---|
| Per request | Pass a proxies dictionary to the individual request |
| Session | Assign proxies to Session.proxies when several authorized requests share settings |
| Environment | Use HTTP_PROXY and HTTPS_PROXY only in a controlled runtime environment |
| SOCKS5 | Install requests[socks]; socks5 uses local DNS and socks5h resolves through the proxy endpoint |
| Reliability | Set connect/read timeouts, catch Requests exceptions, and verify a permitted response |
Keep PROXY_SCHEME, PROXY_HOST, PROXY_PORT, PROXY_USER, and PROXY_PASSWORD as placeholders in shared code. Build the endpoint at runtime from protected values, and do not print the final URL when it contains credentials.


