DEV Community

Cover image for ProxyError Max retries exceeded: read the Caused by part
proxymint
proxymint

Posted on Originally published at proxymint.com

ProxyError Max retries exceeded: read the Caused by part

Originally published on proxymint.

ProxyError Max retries exceeded often means no retry ran at all. See what each cause prints in requests and urllib3, tested, and which failures to retry.

TL;DR

  • Skip the first half of the message and read what follows Caused by. Seeing Tunnel connection failed: 407 means fix your login, and Connection refused means a dead host or port. A 502 is easier to misread, since it can point past the proxy at the target.
  • Max retries exceeded doesn't mean anything was retried. requests defaults to zero retries, so the test proxy saw one connection. Raising the count won't rescue a wrong password either.
  • If you only catch ProxyError, you're missing failures. A proxy that closes after CONNECT raised ConnectionError on urllib3 2.8.0, and a silent one raised ReadTimeout. SOCKS5 goes further, with no failure in the test raising ProxyError at all.
  • Setting connect=0 won't stop a 407 being resent. A refused CONNECT spends the other counter, and Retry with total=3 sent it four times with no pause between them.

Key takeaways

  • Log the inner exception as well as the class name. Most proxy failures share ProxyError or ConnectionError at the top, so that's the only way you can tell a typo in a password from a dead port.
  • Run the proxy through curl before editing Python. A printed 000 407 is a login problem. A curl that tunnels fine puts the bug in your environment or code.
  • Check HTTPS_PROXY even if your code sets a proxy. It beat session.proxies in the test, and so did an empty proxies dict. Passing proxies= on the call won, and proxies with https set to None went direct.
  • Use Retry(total=3, read=0, other=0, backoff_factor=0.5) on the adapter. A refused port got four tries and a 407 went out once. For 429, 500 and 502 to 504, a small wrapper can retry by reading the status.
  • Recheck your except blocks if you move off urllib3 1.26. A proxy connect timeout went from ConnectTimeout to ProxyError, and a silent proxy went the other way, to ReadTimeout.

Read the full guide on proxymint, with the commands, tables and sources.

Top comments (0)