Python Glossary Русский

all Python 3socketfunction

socket.gethostbyname_ex()

names and addresses

An extended gethostbyname: returns (primary name, aliases, all IPv4 addresses of the host).

Syntax

hostname, aliases, addresses = socket.gethostbyname_ex(hostname)

Use it when a name has several addresses (DNS load balancing): gethostbyname returns just one of them. The same limitation applies: IPv4 only, IPv6 addresses never appear in the list. The order is chosen by the DNS server and may change between queries, so always taking [0] is not the same as 'the main address'.

Examples

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

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy