all Python 3socketexception
socket.gaierror
exceptions
An address resolution error from getaddrinfo() and getnameinfo(): name not found, DNS unreachable, family unsupported.
Syntax
except socket.gaierror as e: ...
A subclass of OSError but not of ConnectionError: 'could not find the address' is a different failure from 'the server refused', and a ConnectionError handler will not catch it. Its errno is an EAI_* code, negative on Linux (-2 for 'name not found') and different on macOS, so compare with constants like socket.EAI_NONAME, never with numbers.
Examples
import socket
try:
socket.getaddrinfo('not-an-ip', 80, flags=socket.AI_NUMERICHOST)
except socket.gaierror as e:
print(e.errno == socket.EAI_NONAME) # → True
print(isinstance(e, OSError), isinstance(e, ConnectionError)) # → True False
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy