Python Glossary Русский

all Python 3socketexception

socket.herror

exceptions

A host lookup error from the older functions gethostbyaddr() and gethostbyname_ex(), for example an address with no name.

Syntax

except socket.herror as e: ...

A subclass of OSError: errno holds the h_errno code from the C library and strerror its description. Do not confuse it with gaierror: herror comes from the gethostby* family, gaierror from getaddrinfo and getnameinfo; to catch any resolution failure, except OSError is enough. A reverse lookup of an address with no PTR record is its most common source.

Examples

import socket
print(issubclass(socket.herror, OSError))  # → True
print(issubclass(socket.herror, socket.gaierror))  # → False
try:
    raise socket.herror(1, 'Unknown host')
except OSError as e:
    print(type(e).__name__, e.errno, e.strerror)  # → herror 1 Unknown host

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy