Skip to main content

How to Catch a Connection Error in Python with requests

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

When the server a program calls isn't running, requests can't connect, and the program stops with a long traceback ending in requests.exceptions.ConnectionError: ... Max retries exceeded with url. Catching requests.ConnectionError turns that into one clear message. This guide calls a port where nothing listens and catches the error, with real output from Python 3.15.2 and requests 2.34.2.

Syntax​

Syntax
try:
response = requests.get(url, timeout=seconds)
except requests.ConnectionError:
... # what to do when the server can't be reached
PartWhat it means
requests.ConnectionErrorrequests' error for a request that couldn't connect: nothing listening, an unknown host, no network
except requests.ConnectionError:The lines below it run instead of a crash

The requests quickstart: "In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise a ConnectionError exception."

Prerequisites​

  • Python 3 with the requests package in your project, for example with uv add requests.

The Error Without the Fix​

Port 9 on your own computer has nothing listening on it, which makes it a reliable way to see the error. This one line asks it for a page:

connection_error_uncaught.py
import requests

requests.get("http://127.0.0.1:9", timeout=5)
Output (last line)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=9): Max retries exceeded with url: / (Caused by NewConnectionError("HTTPConnection(host='127.0.0.1', port=9): Failed to establish a new connection: [Errno 61] Connection refused"))

Connection refused is the cause: the computer answered, but no program was listening on that port.

Step 1: Catch the Error​

connection_error.py
import requests

try:
requests.get("http://127.0.0.1:9", timeout=5)
except requests.ConnectionError:
print("Can't connect to the server. Is it running?")

The connection_error.py script in a macOS-style code window: requests.get to http://127.0.0.1:9 with timeout 5 inside try, and except requests.ConnectionError printing Can't connect to the server. Is it running?

Step 2: Run It​

Terminal
uv run python connection_error.py
Output
Can't connect to the server. Is it running?

The script's output in a terminal: Can't connect to the server. Is it running?

The same failure, one readable line, and the program carries on after the except.

Common Mistakes​

MistakeWhat happensDo this instead
except ConnectionError: without requests.Python's built-in ConnectionError is a different class, so the error isn't caught: issubclass(requests.ConnectionError, ConnectionError) printed Falseexcept requests.ConnectionError:
Expecting it for a 404 or 500Those are answers from a server that was reached, so there's no ConnectionErrorCheck response.status_code

Troubleshooting​

SymptomLikely causeFix
[Errno 61] Connection refusedNothing listens on that port: the server isn't running, or the port is wrongStart the server, or check the port in the address
ConnectionError for a host nameThe name doesn't resolve: http://no-such-host.invalid raised ConnectionError tooCheck the spelling of the host

FAQ​

Do I need a separate except for an unknown host?​

No. A refused connection and an unknown host both raised requests.ConnectionError in the example, so one except covers both.

Is a timeout a ConnectionError?​

Only a timeout while connecting: issubclass(requests.ConnectTimeout, requests.ConnectionError) printed True, so except requests.ConnectionError: catches it. A timeout while waiting for the answer, ReadTimeout, printed False; catch it with except requests.Timeout:.

Conclusion​

A request to 127.0.0.1:9 failed with ConnectionError: ... Connection refused, and except requests.ConnectionError: turned it into Can't connect to the server. Is it running?. Python's built-in ConnectionError wouldn't have caught it.

Additional Resources​