all Python 3socketfunction
socket.gethostbyname()
names and addresses
Translates a host name into an IPv4 address string such as '93.184.216.34'. It does not support IPv6.
Syntax
socket.gethostbyname(hostname) -> str
The function only knows IPv4: for a host reachable only over IPv6, and for the address '::1', it raises gaierror, so new code uses getaddrinfo(). A numeric address is returned as is, without DNS. 'localhost' usually gives '127.0.0.1', but gethostbyname(gethostname()) on Debian and Ubuntu often returns '127.0.1.1', which is not the machine's LAN address.
Examples
import socket
print(socket.gethostbyname('127.0.0.1')) # → 127.0.0.1
try:
socket.gethostbyname('::1')
except socket.gaierror:
print('IPv6 не поддерживается') # → IPv6 не поддерживается
import socket
print(socket.gethostbyname('localhost')) # → ? 127.0.0.1
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy