Skip to content
Back to Blog
November 29, 20265 min read25 views

Python Requests Proxy Setup: HTTP and SOCKS5 Examples

Configure Python Requests with an HTTP or SOCKS5 proxy, keep credentials out of source control, set timeouts and verify the actual response before increasing load.

WhiteProxies
Publisher · Editorial policy
Share:
Editorial illustration: Python Requests Proxy Setup: HTTP and SOCKS5 Examples
AI-generated conceptual illustration; not a product screenshot or test result.

Python Requests can send HTTP requests through a proxy using a proxies mapping. Start with one endpoint, one destination and explicit timeouts. Once that connection returns the content you expect, add the rest of your application.

The examples below are for endpoints you own or are authorized to access. They demonstrate client configuration, not a benchmark or a guarantee that a particular destination will accept a proxy connection.

What do you need before running the example?

Install Python and Requests in your project's virtual environment. Have the current proxy host, port and authentication method from your provider's generator. For WhiteProxies, confirm whether the selected plan uses credentials or IP authorization; the method varies by product.

python -m venv .venv
# macOS/Linux:
source .venv/bin/activate
# Windows PowerShell (use instead of source):
.venv\Scripts\Activate.ps1
python -m pip install requests

Set PROXY_URL in the environment where the script runs. An HTTP gateway URL typically looks like http://USERNAME:PASSWORD@HOST:PORT. For an IP-authorized gateway, omit the credentials after authorizing the machine's public outbound IP. Replace the placeholders locally and avoid committing a completed URL to a repository.

Set the variable in the same terminal session before running the Python script. These values are placeholders:

# macOS/Linux
export PROXY_URL="http://USERNAME:PASSWORD@HOST:PORT"
# Windows PowerShell
$env:PROXY_URL="http://USERNAME:PASSWORD@HOST:PORT"

Use the proxy for both HTTP and HTTPS destinations

import os
import requests

proxy_url = os.environ["PROXY_URL"]
proxies = {"http": proxy_url, "https": proxy_url}

with requests.Session() as session:
    session.trust_env = False
    response = session.get(
        "https://api64.ipify.org?format=json",
        proxies=proxies,
        timeout=(10, 30),
        allow_redirects=False,
    )
    response.raise_for_status()
    print(response.json())

The universal ipify endpoint can return an IPv4 or IPv6 address. Use api6.ipify.org only when intentionally verifying an IPv6 exit. The timeout tuple sets connection and read timeouts; it is not a total deadline for the whole download. If your deployment needs a custom CA bundle, pass its path using verify: trust_env = False also ignores environment CA configuration.

This small example uses an IP-check endpoint to show the public exit address. Run it sparingly, then replace the destination with a test URL you control. trust_env = False keeps unrelated proxy settings from the surrounding environment out of this specific test. The explicit mapping makes the route being tested easier to inspect.

The https key describes the destination scheme. It does not automatically mean the proxy URL must start with https://. An HTTP proxy can tunnel an HTTPS connection using CONNECT. Use the connection scheme documented for your proxy endpoint.

How do I use SOCKS5 with Requests?

Requests supports SOCKS proxies through an optional dependency. Install the SOCKS extra in the same environment that runs your script, then set a matching proxy URL:

python -m pip install 'requests[socks]'
# Set PROXY_URL to the generated SOCKS5 endpoint URL.
# Example shape: socks5h://USERNAME:PASSWORD@HOST:PORT

With socks5h, hostname resolution is delegated through the proxy. With socks5, resolution happens locally. Choose intentionally, especially when comparing DNS behavior. The hostname, port and credentials must still belong to a service that supports the selected protocol.

Keep credentials out of logs

Reading a proxy URL from an environment variable avoids embedding it in the example source file, but it does not make that value harmless. Shell history, debug output and exception messages can still expose secrets. Redact the proxy password in logs and use your deployment platform's secret settings for production jobs.

If your username or password contains reserved URL characters, encode those values before constructing a URL. For larger applications, put URL construction in one tested helper instead of duplicating string concatenation across scripts.

Check the response, not just the exit IP

An exit-IP check confirms that the test request used an outward-facing route. The next step is to verify the actual authorized target. Keep redirects visible during diagnosis: a redirect may move the request to another hostname with different address-family support or access requirements.

A 200 status is not sufficient if the body is a login screen or error message. Check the content type and a field or element your application expects. Record a redacted target identifier, status, elapsed time and validation result so you can distinguish a valid response from a transport success.

Common failures and the next useful check

SymptomCheck first
Proxy connection exceptionHost, port, selected protocol and network reachability
407 authentication responseProxy credentials or source-IP authorization
Read or connect timeoutWhere the request stalled; test one request before adding concurrency
403 or 429 from the targetTarget permissions and rate limits, not just proxy credentials
IPv6 route fails on one hostnameExact destination's IPv6 compatibility

Do not disable TLS verification to make a failing proxy example appear to work. Diagnose certificate and trust configuration separately. Keep retry limits finite, honor destination limits and avoid automatically retrying writes that could be duplicated.

Common questions

Does a Requests Session keep the same exit IP?

It can reuse client connections and cookies, but the provider's session and rotation controls determine the exit behavior. A Python Session is not a substitute for selecting a sticky proxy session.

Can I add hundreds of threads immediately?

Start with one successful request and increase gradually within your plan and destination limits. Monitor valid responses, latency, bandwidth and errors together.

Generate a current endpoint from your subscription, check the target's IPv6 DNS records when relevant, and compare IPv4, IPv6 or GB plans against your application's requirements.

Sources

IP-check endpoint reference: ipify official documentation.

Related Topics:
Python requests proxyPython proxy authenticationrequests SOCKS5 proxyPython HTTP proxyrequests proxy timeout

Ready to Try WhiteProxies?

Compare IPv4, IPv6 and residential GB plans for your destination, traffic and concurrency needs.