How to Use a Proxy in Python Requests (HTTP and SOCKS5)
To test a proxy in Python Requests, send the same HTTPS request twice and compare the IP each one returns. Start with the proxy host and port from your provider, then add credentials or switch to SOCKS5 if your endpoint requires it.
Install Python Requests
For an HTTP proxy, install Requests in the Python environment that runs your script.
python -m pip install requests
If you use a SOCKS5 proxy, install the SOCKS extra in that environment.
python -m pip install "requests[socks]"
Send an HTTPS request through an HTTP proxy
Replace gateway.example:8080 with the host and port from your provider. This script makes two requests to the same IP lookup, first directly and then through your proxy. Run it on the machine that will run your Python app.
import requests
probe_url = "https://testmyproxies.com/api/my-ip"
proxy_url = "http://gateway.example:8080"
with requests.Session() as session:
session.trust_env = False
direct = session.get(probe_url, timeout=(5, 15))
direct.raise_for_status()
print("Direct:", direct.json()["ip"])
through_proxy = session.get(
probe_url,
proxies={"https": proxy_url},
timeout=(5, 15),
)
through_proxy.raise_for_status()
print("Via proxy:", through_proxy.json()["ip"])
The https key selects requests to HTTPS destinations. The http:// in the proxy URL tells Requests how to connect to the proxy itself. The proxy uses CONNECT to open a tunnel to the HTTPS destination. Use https:// in the proxy URL if your provider specifies a TLS connection to the proxy. If the script also makes requests to http:// destinations, add an http key to cover those requests. HTTP, HTTPS and SOCKS5 proxy protocols
session.trust_env = False keeps environment proxy settings out of this comparison. It also ignores environment-based CA bundles and .netrc authentication. If your application needs a private CA bundle, pass the trusted bundle path explicitly as verify=.... Requests proxy configuration
Add a proxy username and password
In the script above, replace the proxy_url assignment with these lines. Replace the example hostname and port here too. The script prompts for the password at runtime and encodes characters such as @ before building the proxy URL.
from getpass import getpass
from urllib.parse import quote
username = quote(input("Proxy username: "), safe="")
password = quote(getpass("Proxy password: "), safe="")
proxy_url = f"http://{username}:{password}@gateway.example:8080"
Keep the resulting URL out of logs because it contains the credentials. If your provider authorizes access by IP, keep the credential-free URL and compare its allowlist with the Direct IP printed by the script. An online proxy tester sends requests from its own server, so its source IP can differ from your Python app’s. How to diagnose HTTP 407
Use SOCKS5 and choose where DNS runs
After installing requests[socks], replace the proxy_url line with the SOCKS5 address. The https key in the request stays the same because the IP lookup still uses HTTPS.
proxy_url = "socks5h://gateway.example:1080"
Use socks5h:// when the proxy should resolve the destination hostname. With socks5://, the client resolves it locally. If your SOCKS5 endpoint requires a login, use the same encoded username and password from the earlier example with the appropriate SOCKS scheme and port. SOCKS support in Requests
Confirm the exit IP and diagnose errors
Compare the Via proxy IP with the exit you expect from your provider. Both addresses can match when the Python client and the proxy share the same public egress. If the result is unexpected, compare it with another check using the same host, port and protocol. Rotating gateways can return different IPs across requests, so keep track of the session settings when you compare results.
- InvalidSchema with “Missing dependencies for SOCKS support”. Run
python -m pip install "requests[socks]"with the interpreter that runs your app. This often comes up when a terminal and an IDE use different virtual environments. - ProxyError with a 407 during CONNECT. Check the proxy credentials and authentication method. SOCKS5 authentication fails during its handshake and can appear as a proxy error without an HTTP status.
- Connection refused. Confirm the proxy host, port and scheme in your provider’s connection details. Run the same request from the same machine with cURL to isolate the Python configuration.
- ConnectTimeout or ReadTimeout. The tuple sets separate connect and read timeouts in seconds. Check whether the endpoint responds directly, then retest through the proxy to see which path stalls.
- SSLError. Check the hostname and certificate chain and confirm where TLS is failing. Leave certificate verification enabled and use the CA bundle your organization or provider specifies for any managed proxy that performs TLS inspection.
- HTTPError with a 403 or 429 from the destination. Check the site’s access policy or rate limit. For a target-side 429, read
Retry-Afterwhen present. 403 Forbidden · 429 Too Many Requests
For a second trace, use the same proxy address and HTTPS URL in the cURL proxy test. To compare several proxy endpoints or inspect their reported exit locations, run a test on TestMyProxies.