← Back to Error Guide
TimeoutError

How to Fix TimeoutError

Understanding and resolving TimeoutError in Python

What is TimeoutError?

A TimeoutError occurs when a specified time limit is exceeded during an operation.

Common Causes

Common causes of this error...

How to Fix

Wrong Code

# Code that causes the error
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
sock.connect(('example.com', 80))

Correct Code

# Fixed code
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
try:
    sock.connect(('example.com', 80))
except socket.timeout:
    print('Connection timed out. Retrying...')
    sock.settimeout(10)
    sock.connect(('example.com', 80))

More Python Error Guides