Python Glossary Русский

all Python 3socketfunction

socket.gethostbyaddr()

names and addresses

Reverse lookup: for an IP address it returns (hostname, list of aliases, list of addresses).

Syntax

hostname, aliases, addresses = socket.gethostbyaddr(ip_address)

It handles both IPv4 and IPv6, but it queries DNS and waits seconds when the server is unreachable. Most addresses have no reverse record, and then socket.herror is raised instead of an empty result. The answer depends on /etc/hosts and DNS, so tests should not rely on it. If you only need the numeric form, getnameinfo() with NI_NUMERICHOST does no network access.

Examples

import socket
name, aliases, addresses = socket.gethostbyaddr('127.0.0.1')
print(name)  # → ? localhost
print(addresses)  # → ? ['127.0.0.1']

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy