Basic configuration
- Format the ip:port:user:pass string into a URL: proxy_url = "http://user:pass@IP:PORT".
- Declare both keys: proxies = {"http": proxy_url, "https": proxy_url}. The "https" key still points to a URL starting with http:// because ONET proxies are HTTP proxies; requests automatically uses CONNECT for HTTPS sites.
- Send the request: r = requests.get("https://api.ipify.org", proxies=proxies, timeout=(10, 30)); print(r.text). The result should be the proxy IP.
- Do not write "https": "https://IP:PORT". ONET proxies do not accept TLS connections to themselves and will return an SSL error or close the connection.
How do you use proxies with special characters in the password in requests?
URL-encode the username and password before assembling: from urllib.parse import quote; proxy_url = f"http://{quote(user, safe='')}:{quote(pw, safe='')}@{ip}:{port}". Without this step, @ or : characters in the password will cause requests to misparse the proxy address.
Using Session and retries
- Create s = requests.Session(); s.proxies.update(proxies). All requests made through s share the proxy and maintain connections, which is faster than calling requests.get repeatedly.
- Mount an HTTPAdapter with Retry(total=3, backoff_factor=1, status_forcelist=[429, 502, 503]) to automatically retry on transient errors.
- Always set a timeout. Without a timeout, a hanging connection can freeze your program indefinitely.
- The HTTPS_PROXY environment variable is also read by requests, unless you set s.trust_env = False.
With httpx
Starting from httpx 0.26, pass the proxy using the proxy parameter: httpx.Client(proxy="http://user:pass@IP:PORT", timeout=30). Older versions use proxies={"all://": url}. httpx supports async (httpx.AsyncClient) if you need to run concurrent requests.
What proxy errors does Python requests commonly encounter?
The three common errors are 407 due to an incorrect username or password, timeouts from failing to reach the proxy, and 429 from target website rate limits.
ProxyError … 407 Proxy Authentication Required
Incorrect username or password, or special characters were not encoded.
ConnectTimeout hoặc ProxyError … timed out
Cannot reach the proxy: incorrect IP, port, or network blockage. Verify using curl.
HTTP 429 từ trang đích
Rate-limited by the destination site. Reduce requests per IP, add delays, or distribute the load across multiple proxies.
Don't have a proxy? Buy dedicated proxies with automatic activation
Dedicated IPv4 proxies across 9 countries from 20.000₫/IP/month when purchasing 2.000 IPs or more (39.000₫ for 1 IP), plus Vietnam and US static ISP proxies. HTTP(S) and SOCKS5 with username and password, unlimited bandwidth, automatically provisioned after payment. Vietnam proxies currently support HTTP(S) only, without SOCKS5.
Buy proxies now