Skip to content

Python requests Proxy Setup: proxies Dict, Environment Variables and SOCKS5

The requests library accepts a proxy through the proxies parameter, through environment variables or through a Session. Below is each way with a short example, the rules from the Requests documentation and how to check the result.

The proxies parameter

The Requests documentation says you can configure a single request with the proxies argument, a dict whose key http or https names the scheme of the destination. Note that even for the https key the documentation examples give the proxy URL with the http:// scheme. The documentation stresses that proxy URLs must include the scheme.

If the username or password has special characters, encode them. The Python documentation says urllib.parse.quote keeps / as safe by default (safe='/'), so pass safe='' for a password.

The proxies parameter: example

import requests
from urllib.parse import quote

user = quote("USERNAME", safe="")
password = quote("PASSWORD", safe="")
proxy = f"http://{user}:{password}@proxy.proxy302.com:2222"
proxies = {"http": proxy, "https": proxy}

r = requests.get("https://ipinfo.io/json", proxies=proxies, timeout=(5, 20))
print(r.json())

Replace USERNAME and PASSWORD with the values from My assets. Proxy302 HTTP is on port 2222.

Session and environment variables

When proxies is not passed to a request, Requests relies on the standard environment variables http_proxy, https_proxy, no_proxy and all_proxy, and the upper-case variants. The documentation warns that storing a username and password in an environment variable or a version-controlled file is a security risk and is highly discouraged.

For a Session the documentation shows session.proxies.update(proxies) but warns that values set this way may be overwritten by environmental proxies. To make sure your own proxies are used when environment variables exist, pass proxies explicitly on every request.

Session and environment variables: example

import requests

session = requests.Session()
r = session.get("https://ipinfo.io/json", proxies=proxies, timeout=(5, 20))
print(r.json())

proxies is the same dict as in the example above.

SOCKS5

The Requests documentation says SOCKS support is an optional feature that needs extra libraries: python -m pip install 'requests[socks]'. After that a SOCKS proxy is used just like an HTTP one, with the socks5:// scheme in the URL.

The scheme matters: with socks5 DNS resolution happens on the client, and with socks5h it happens on the proxy. The documentation points to socks5h if you want domains resolved on the proxy server.

SOCKS5: example

user = quote("USERNAME", safe="")
password = quote("PASSWORD", safe="")
socks = f"socks5h://{user}:{password}@proxy.proxy302.com:3333"
proxies = {"http": socks, "https": socks}

r = requests.get("https://ipinfo.io/json", proxies=proxies, timeout=(5, 20))
print(r.json())

This continues the first example (it needs import requests and from urllib.parse import quote) and needs requests[socks] installed. Proxy302 SOCKS5 is on port 3333; HTTP is on port 2222.

Timeouts

According to the Requests documentation, requests do not time out by default, and without a timeout your code may hang for minutes or more. A single timeout value applies to both connect and read; a tuple (connect, read) sets them separately. The connect timeout is the wait for the connection to be established, and the read timeout is the wait for the server's response. If a domain has several addresses, they are tried one after another, so the effective total can be a multiple of the value.

Check the exit

Request https://ipinfo.io/json as in the examples above. The country and org fields should describe the proxy, not your own connection. If your own address shows, the proxy was not applied: check the keys of the proxies dict and that it is passed to this request. The proxy location guide explains the fields.

Typical errors

In the Requests exceptions module, ProxyError is a subclass of ConnectionError described as "A proxy error occurred". ConnectTimeout means the request timed out while connecting, and requests that produced it are safe to retry; ReadTimeout means the server sent no data in the allotted time. Check the scheme in the proxy URL, the port (HTTP 2222, SOCKS5 3333) and the username and password. Status 407 means the proxy did not accept the credentials; see the authentication guide, and the connection failed guide has the checks step by step. The Requests documentation also notes that using a proxy for https connections typically requires your local machine to trust the proxy's root certificate.

How to do it with Proxy302

The requests library needs the address, port, username and password of a Proxy302 proxy. Rotating residential traffic is billed per GB and the exit country is set when you generate the proxy; HTTP is on port 2222 and SOCKS5 on port 3333 of proxy.proxy302.com. A static ISP IP stays fixed for the whole lease term and does not rotate. The table shows exits in 7 countries from our latest hourly check; the probe sends its request to ChatGPT, which is only an example target of a network check. A proxy does not change ChatGPT's terms, and OpenAI's list of supported countries still applies. Details are on the rotating residential page and in the proxy location guide.

Measured exits: ChatGPT from 7 countries

Measured exits: ChatGPT from 7 countries
ItemCityNetwork (ISP)ASN
United StatesNeenah, WisconsinCharter Communications IncAS10796
United KingdomCowplain, EnglandTalkTalk Communications LimitedAS13285
GermanyKöln, North Rhine-WestphaliaNetCologne Gesellschaft fur Telekommunikation mbHAS8422
JapanTokyo, TokyoKDDI CORPORATIONAS2516
SingaporeSingapore, SingaporeSingtel Fibre BroadbandAS9506
BrazilMauá, São PauloTELEFÔNICA BRASIL S.AAS27699
IndonesiaBogor, West JavaPT Mora Telematika IndonesiaAS131111

Hourly check of the Proxy302 probe, 25 October 2026, 08:29 UTC: one request to ChatGPT per country through a dynamic residential exit, using the public Proxy302 entry. Only the first three octets of an IP are public. One sample per country, not an average; later checks will differ.

How to do it with Proxy302: steps

  1. Step 1

    Open the rotating residential page, choose the line, and keep Protocol on HTTP or choose SOCKS5, then click Review cost and generate.

  2. Step 2

    Open My assets (the console page that holds everything you bought) and copy HOST, PORT, USERNAME and PASSWORD. The Connect page shows a ready example with your own endpoint.

  3. Step 3

    Enter the details in the browser or tool you are setting up.

  4. Step 4

    Confirm the exit with https://ipinfo.io/json through the proxy.

  5. Step 5

    From mainland China, start the helper tool from the Connect page first and keep it open while you use the proxy.

Prices and limits

The Budget line costs US$1.50 per GB, US$1.35 per GB in a 100 GB package and US$1.20 per GB at 300 GB. There is no free trial, and the smallest purchase is 1 GB. Which exit locations are available changes daily.

FAQ

Which scheme goes with the https key?

The scheme of the proxy itself. The Requests documentation examples give the proxy URL with http:// for the https key.

How do I use SOCKS5 with requests?

Install requests[socks] and use the socks5:// or socks5h:// scheme. With socks5h the proxy resolves domains.

What do I do with special characters in the password?

Encode it with urllib.parse.quote and safe='', otherwise the / character stays as it is.

Why does requests still use a proxy from the environment?

If proxies is not passed to the request, Requests reads the environment variables. For a Session the documentation advises passing proxies on every request.

Reference links

5
  1. Requests documentation: Advanced Usage (Proxies, SOCKS, Timeouts)

    Official documentation · Confirmed on:

    Read on 25 October 2026: the proxies argument, Session and the overwrite warning, environment variables, URLs with a scheme and login, requests[socks], socks5 and socks5h, timeouts, and trusting the proxy's root certificate for https.

  2. Requests source: requests.exceptions

    Official documentation · Confirmed on:

    Read on 25 October 2026: ProxyError as a subclass of ConnectionError, ConnectTimeout (safe to retry) and ReadTimeout.

  3. Python documentation: urllib.parse.quote

    Official documentation · Confirmed on:

    Read on 25 October 2026: quote() keeps / safe by default (safe='/'), and the safe argument lists characters that are not encoded.

  4. Proxy302 product facts and price list

    Product observation · Confirmed on:

    Product facts file of the site (values checked against the console, the backend and the price table on 18-19 October 2026): products, prices in US$ (1 PTC = US$1), lease terms, protocols and ports, countries, quantity limits, no free trial.

  5. Proxy302 hourly connectivity check

    Product observation · Confirmed on:

    Public JSON of the probe at proxy302.com/live/connectivity/chatgpt.json, read 25 October 2026, 08:29 UTC: one request per exit country through a dynamic residential exit; exit city, ASN, network and response time.

Ready to try it?

Create an account, top up once, then pick rotating residential, static ISP or datacenter IPs and have one running in your browser within minutes.