Decoding "ProxyError: Max retries exceeded" in Python requests
By the ProxyV4 Engineering Team · Updated:
requests.exceptions.ProxyError: ... Max retries exceeded means the request never got through your proxy, and the real reason sits in the last parenthesis of the message, not in the words "max retries". Requests does not retry by default; that phrase is just how urllib3 wraps the final failure. Read the inner error, such as Connection refused, Tunnel connection failed: 407 or timed out, and it tells you which hop broke.
Anatomy of the traceback
A typical message, trimmed, looks like this:
requests.exceptions.ProxyError: HTTPSConnectionPool(host='example.com', port=443):
Max retries exceeded with url: / (Caused by ProxyError('Unable to connect to proxy',
NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f...>:
Failed to establish a new connection: [Errno 111] Connection refused')))Three parts matter. host='example.com' is the destination, not the proxy, which confuses many people. Unable to connect to proxy (older urllib3 1.26 prints Cannot connect to proxy.) says the failure happened between your machine and the proxy, or during the proxy's handshake. The innermost text is the actual cause. Everything below is organized by that innermost text.
Match the inner error to the broken hop
| Inner error text | Hop that failed | Most likely cause |
|---|---|---|
[Errno 111] Connection refused | Your machine to proxy | Wrong port, proxy expired, or a mode that does not listen on that port |
Failed to resolve / Name or service not known | DNS lookup of the proxy host | Typo in the proxy hostname, or a whole ip:port:user:pass line used as the host |
ConnectTimeoutError ... timed out | Your machine to proxy | Firewall dropping the port, wrong IP, or a network that blocks outbound proxies |
Tunnel connection failed: 407 Proxy Authentication Required | Proxy login | Wrong or missing username and password |
Tunnel connection failed: 502 or 503 | Proxy to destination | The proxy could not reach the site |
SSLError with wrong version number | TLS to the proxy | Proxy URL starts with https:// but the proxy speaks plain HTTP |
Cause by cause: checks and fixes
Connection refused or Cannot connect to proxy
Nothing is listening on that IP and port. Test the socket alone, outside Python: nc -vz IP PORT on Linux or macOS, or Test-NetConnection IP -Port PORT in PowerShell. If that also fails, check that the proxy is active and that you copied the current port; providers that let you switch protocol modes may move the proxy to another port after a change. If the socket opens, the proxy is up and the problem is in how requests is talking to it.
Tunnel connection failed: 407
The proxy answered and refused the login. Make sure credentials are in the URL, http://USER:PASS@IP:PORT, and that special characters in the password are percent-encoded. urllib.parse.quote(password, safe='') turns @ into %40 and : into %3A. One subtlety: for a plain http:// destination there is no tunnel, so you get no exception at all, just response.status_code == 407. The 407 guide covers the other ways credentials go missing.
Timeouts: ProxyError or ConnectTimeout
Depending on your urllib3 version, a timeout while reaching the proxy surfaces either as ConnectTimeout or as ProxyError wrapping ConnectTimeoutError. Either way it is a reachability problem, not a login problem. Always pass timeout=(5, 20) so the failure arrives in seconds instead of hanging, and if only some runs time out, see the proxy timeout page for the network-side causes.
SSLError and the https:// proxy URL
Writing 'https': 'https://USER:PASS@IP:PORT' makes urllib3 open TLS to the proxy itself. A normal HTTP proxy cannot answer that, so the handshake fails with wrong version number; recent urllib3 versions even add a hint telling you to change the proxy URL to HTTP. Keep http:// as the proxy scheme for both keys. The https key name refers to the destination, and your HTTPS traffic still runs end to end inside the CONNECT tunnel. An SSLError mentioning CERTIFICATE_VERIFY_FAILED is different: the tunnel worked and the site's certificate did not validate, usually because something on your network intercepts TLS.
Environment variables and trust_env
Requests reads HTTP_PROXY, HTTPS_PROXY, ALL_PROXY and NO_PROXY from the environment, in upper or lower case. A stale variable left by a VPN client, a CI job or a corporate shell profile can send traffic to a proxy you forgot about, or override the one you set on a Session. The requests documentation notes that environment proxies can take precedence over session.proxies, so pass proxies= on each call when you need certainty, or switch the environment off:
import os, requests
print({k: v for k, v in os.environ.items() if 'proxy' in k.lower()})
s = requests.Session()
s.trust_env = False # ignore *_PROXY variables and .netrc
s.proxies = {
'http': 'http://USER:PASS@IP:PORT',
'https': 'http://USER:PASS@IP:PORT',
}
print(s.get('https://proxyv4.com/api/my-ip', timeout=(5, 20)).text)If the printed IP is the proxy address, routing is correct. NO_PROXY works in the other direction: a destination listed there skips the proxy entirely, which can make a test look like it passed when it never used the proxy.
Stop debugging the proxy and start using it
Half of these errors trace back to the line itself: an expired proxy, a moved port or a mistyped password. Before blaming your code, paste the line into the proxy checker. If it reports Working with the expected exit IP, every remaining error is in your script.
When you need proxies for scripts, a fixed address helps with debugging as much as with production: the IP in your logs is the IP the site saw. ProxyV4 private datacenter IPv4 proxies are one tenant per IP, support HTTP(S) and SOCKS5 with username and password (both can run on the same port), keep the same IP when renewed before expiry, and do not meter traffic, so a long crawl does not run up a bandwidth bill. They are not a rotating pool; if each request must come from a new IP, this is the wrong product. See live per-IP prices on the pricing page, order one, and run it through the requests setup guide before scaling.
SOCKS5 in requests: socks5 versus socks5h
SOCKS support is optional. Without it, a socks5:// URL raises InvalidSchema: Missing dependencies for SOCKS support; install it with pip install "requests[socks]", which pulls in PySocks.
The scheme then decides where DNS happens. socks5:// resolves the destination hostname on your machine and sends the IP to the proxy. socks5h:// sends the hostname and lets the proxy resolve it, which avoids local DNS leaks and fixes sites your local resolver cannot see. Note that SOCKS failures usually arrive as ConnectionError with a SOCKS message inside rather than as ProxyError, so catch both when you handle errors.
proxies = {
'http': 'socks5h://USER:PASS@IP:PORT',
'https': 'socks5h://USER:PASS@IP:PORT',
}
r = requests.get('https://example.com', proxies=proxies, timeout=(5, 20))Frequently asked questions
Why does requests say max retries exceeded when I set no retries?
The phrase comes from urllib3's MaxRetryError, which wraps the final failure even when zero retries are allowed. Ignore it and read the error in the innermost parenthesis.
Should the https key in proxies use an https:// proxy URL?
Usually not. The key names the destination scheme; the value should be http:// for a normal HTTP proxy, and HTTPS pages are still encrypted through the tunnel.
What is the difference between urllib3 ProxyError and requests ProxyError?
urllib3 raises its own ProxyError when talking to the proxy fails, and requests converts it into requests.exceptions.ProxyError. The inner cause is the same in both.
Why does my proxy work in curl but fail in requests?
Compare three things: an environment variable overriding your setting, an https:// proxy scheme, and an unencoded special character in the password. One of them is the usual culprit.
Do ProxyV4 proxies work with requests over SOCKS5?
Yes, once the order has SOCKS5 enabled in the dashboard and requests[socks] is installed. HTTP mode needs no extra package.
A working proxy line removes the guesswork
ProxyV4 private IPv4 proxies arrive as one ip:port:user:pass line that works with requests over HTTP or SOCKS5, with unmetered traffic. Buy one IP, verify it in the checker, then scale your script.
Get a working proxy