Developer proxy guide

How to Use Proxies with Python Requests

Set up an HTTP or SOCKS5 proxy in Python Requests with the proxies dictionary. Start with a short example that verifies your exit IP, then add authentication and timeouts for your application.

Start with one HTTPS request through an HTTP proxy in the first Requests example, or skip to SOCKS5 setup and DNS if that is your proxy type. If Requests uses a different route from the one you configured, check how Session.trust_env affects proxy selection. For asynchronous work, compare HTTPX and aiohttp; a scheduled crawler has its own Scrapy configuration guide.

Configure a proxy in Python Requests

Install Requests in the Python environment that will run your script with python -m pip install requests. In the proxies dictionary, "http" and "https" match the destination URL. The value is the connection URL for your proxy. For an HTTPS destination through an HTTP proxy, the mapping can look like {"https": "http://proxy.example.com:3128"}. The Requests test results list the library version and show which HTTP and SOCKS routes were exercised.

Start with one endpoint from your private proxies plan and test it against an IP-check page. Do not begin with the final destination. First prove that Python is sending traffic through the proxy.

For this one-request check, enter the full proxy URL at the hidden prompt. Use the scheme supplied for the connection to the proxy. The example sends an HTTPS request to ipify, which returns the public IPv4 address it sees.

from getpass import getpass

import requests

proxy_url = getpass("Proxy URL (input hidden): ").strip()
response = requests.get(
    "https://api.ipify.org?format=json",
    proxies={"https": proxy_url},
    timeout=(5, 10),
)
response.raise_for_status()
print(response.json()["ip"])

For an endpoint with a fixed exit, compare the printed address with the IPv4 address assigned to it. The timeout values allow five seconds to connect and ten seconds between reads.

Proxy username and password format

A common format is protocol://username:password@host:port. If the password includes special characters, encode them correctly or use a safer construction method instead of pasting raw credentials into a string. Never commit proxy credentials into Git.

If you see a 407 error, the proxy rejected authentication. Use the proxy authentication guide and the proxy 407 guide before rewriting your request logic.

  • Confirm host and port
  • Confirm HTTP versus SOCKS support
  • Encode special characters in passwords
  • Set a timeout on every request
  • Log status code and exception type
BuyProxies title card reading How to Use Proxies with Python Requests
BuyProxies title card for the Python Requests proxy guide.

Route HTTPS requests through the proxy

For HTTPS targets, Requests connects through the proxy and then negotiates TLS with the destination. If certificate verification fails, do not immediately disable verification. First confirm whether the destination, corporate network or local environment is changing certificates.

The official Requests proxy documentation is the best external reference for supported proxy behavior. Keep your application code close to those documented patterns.

Configure SOCKS5 for Python Requests

Install the SOCKS extra in the same Python environment with python -m pip install "requests[socks]". Run the same IP-check script and enter a SOCKS endpoint at the hidden prompt, for example socks5h://proxy.example.com:1080. For a password-authenticated endpoint, use the username and encoded password format above.

socks5h:// resolves the destination hostname through the proxy. socks5:// resolves it on the computer running the script. Choose the scheme that matches your application’s DNS requirements. See the Requests SOCKS documentation.

If you do not need SOCKS specifically, HTTP proxies are often simpler for Requests. Compare the protocol decision with the SOCKS5 versus HTTP proxy guide.

Timeouts, retries and status codes

Always set a timeout. Without one, a script can hang and look like a proxy failure when the real issue is a slow destination or network stall. Retry only specific transient errors, and add a delay so you do not turn one failure into a burst.

How do I make a slow proxy test in Requests easier to interpret? Pass a pair of timeout values so the connection and read stages have separate limits. Capture the exception type together with the destination and elapsed time. Requests’ read timeout measures waiting for data, so a response that keeps sending bytes can run longer than that value overall.

Separate proxy connection errors from destination responses. A timeout, 407, 403 and 429 point to different fixes. The proxy error code guide can help classify them.

Three steps for a Python Requests proxy: setup, test and verify
A safe Requests proxy setup includes timeouts, logging and exit-IP checks.

If you are moving this Requests workflow to async I/O, compare the HTTPX proxy configuration and aiohttp proxy examples before reusing client-specific options.

Testing before production

Run the script from the same machine that will execute it in production. A home laptop, CI runner and VPS may have different DNS, firewall and source-IP behavior. If your plan uses IP whitelisting, the production server IP is the one that matters.

After the test passes, record the proxy URL format without the password, the observed exit IP, country, average latency and destination status. That short note saves debugging time later.

Practical rollout checklist

  • Run the timed request from the same Python environment and machine that will use the proxy.
  • Compare the returned IPv4 address with the expected exit IP. Then test one request to your destination and record its status.
  • Record the proxy protocol, test date and any exception type. Keep real credentials out of logs, screenshots and tickets.

FAQ

How do I set a proxy in Python Requests?

Pass a proxies dictionary with http and https keys to requests.get or a Session.

Why does Requests show 407?

The proxy credentials are missing, malformed or not accepted.

Does Requests support SOCKS5?

Yes, when the optional SOCKS dependency is installed.

Should I disable SSL verification?

No. Fix certificate or environment issues before disabling verification.

Can I use the same proxy for HTTP and HTTPS?

Often yes, if the provider supports it and the URL format is correct.

Build the first script around one stable endpoint, test it with the proxy checking guide, then scale only after timeouts and errors are logged clearly.

Verified Requests configuration

On , Requests 2.34.2, urllib3 2.8.0 and PySocks 1.7.1 ran on Python 3.12.14 for Windows. Sixteen controlled cases exercised requests.get and Session, HTTP and HTTPS destinations through HTTP proxies and socks5h, encoded synthetic credentials, wrong authentication, an untrusted certificate, a destination 403 and a first 302 response without following it. All sixteen returned the expected outcome.

The fixtures were local and did not forward requests to the internet. This checks client configuration; it does not measure BuyProxies routing, exit IPs, speed or your destination. Certificate verification stayed enabled. Exact versions and limits are recorded in the compatibility matrix.

Scroll to Top