TimeoutError in Python — What It Means and How to Fix It
What Python's TimeoutError exception means and how to resolve it.
Published October 29, 2026
TimeoutError is raised when an operation such as a socket call or asyncio wait takes longer than its timeout.
What it means and why it happens
Typical causes: slow or unreachable servers, firewalls, or timeouts set too low.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
TimeoutError: timed out
Practical examples
Code that triggers it
import socket
socket.create_connection(('10.255.255.1', 80), timeout=1)The fix
import socket
try:
socket.create_connection(('example.com', 80), timeout=5)
except TimeoutError:
print('retry later')asyncio timeout
import asyncio
try:
await asyncio.wait_for(fetch(), timeout=3)
except TimeoutError:
...Common mistakes
- No timeouts at all so calls hang.
- No retries or backoff.
- Timeouts too aggressive for slow networks.
Key takeaways
- TimeoutError: TimeoutError is raised when an operation such as a socket call or asyncio wait takes longer than its timeout.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
What default timeout do requests use?
None; always pass timeout= to requests.get().
How do I find which line caused TimeoutError?
The error message and stack trace show the file and line; start there and work backwards.