Fix "ConnectionError: Max retries exceeded with url"
ConnectionError: Max retries exceeded with url means requests gave up retrying a dead connection. Learn how timeouts, urllib3 retries with exponential backoff, and sessions fix it for good.
You're staring at a traceback that ends with this: requests.exceptions.ConnectionError: Max retries exceeded with url: /api/data (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object ...>: Failed to establish a new connection: [Errno 111] Connection refused')) Your script worked yesterday. Nothing in your code changed. So what broke? Usually, nothing in *your* code β the server hiccupped, a proxy flapped, or a transient network blip killed the connection. The real bug is that your code had no plan for that moment: no timeout, no retries, no backoff. By default, requests retries zero times and waits forever . This tutorial gives it a plan. What you'll learn How to set a timeout in Python requests (and why the default is dangerous) Connection timeout vs read timeout: the two phases of timeout=(connect, read) How to build a urllib3.Retry adapter with exponential backoff How to wire retries into a production-ready requests.Session How to catch Timeout , ConnectionError , and friends without swallowing real bugs π¬ Watch: timeouts & retries in 10 minutes Prefer video? This walkthrough covers the exact failure in this guide β a requests call with no timeout dying on a flaky network β and adds timeouts plus a retry mechanism, the same fix you will build step by step below. β± 10:12 Β· StatLearn Tech β click to play from 4:20, right where the fix starts. Step 1 β How to set timeout in Python requests Install the library (stdlib doesn't include it) and start with the single most important habit: pip install requests requests.get() without a timeout will wait indefinitely if the server accepts the connection and then never answers. Your script hangs, your cron job piles up, your web worker is stuck. The fix is one argument: importβ¦