Python Glossary Русский

all Python 3socketfunction

socket.getnameinfo()

names and addresses

The reverse of getaddrinfo: translates a socket address (host, port) into a pair of strings (host name, service name).

Syntax

host, service = socket.getnameinfo(sockaddr, flags)

With flags=0 it performs a reverse DNS lookup and a service-name lookup, so it depends on the network and /etc/services. The NI_NUMERICHOST and NI_NUMERICSERV flags forbid name lookups, giving the address and port in canonical numeric form with no network access. The port comes back as a string even in numeric mode: '80', not 80.

Examples

import socket
flags = socket.NI_NUMERICHOST | socket.NI_NUMERICSERV
print(socket.getnameinfo(('127.0.0.1', 80), flags))  # → ('127.0.0.1', '80')
print(socket.getnameinfo(('127.0.0.1', 443), 0))  # → ? ('localhost', 'https')

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy