Python Glossary Русский

all Python 3socketfunction

socket.gethostname()

names and addresses

Returns the network name of the machine running the interpreter: a string, not an IP address.

Syntax

socket.gethostname() -> str

This is the short name from the OS settings, often without a domain; getfqdn() gives the full one. Finding 'my IP' through gethostbyname(gethostname()) is unreliable: Debian and Ubuntu yield 127.0.1.1, and a machine with several interfaces has several addresses. The name is set by the user, so on Windows it may even contain non-ASCII characters.

Examples

import socket
print(socket.gethostname())  # → ? my-laptop
print(isinstance(socket.gethostname(), str))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy