Python (Scripting) - hostname (socket)

by
Jeremy Canfield |
Updated: April 01 2024
| Python (Scripting) articles
socket can be used to get the system hostname in a Python script. In this example, the full system hostname will be printed.
#!/bin/python
import socket
print(socket.gethostname())
In this example, only the first field of the hostname will be printed (short).
#!/bin/python
import socket
print(socket.gethostname().split('.', 1)[0])
Did you find this article helpful?
If so, consider buying me a coffee over at