How to Get the System Hostname in Python
Created
Modified
Using gethostname Method
The socket.gethostname()
method returns a string containing the hostname of the machine where the Python interpreter is currently executing. For example,
#!/usr/bin/python3
# Import module
import socket
print(socket.gethostname())
installmd.com
Using platform Module
The Path.resolve()
method returns the computer’s network name (may not be fully qualified!). An empty string is returned if the value cannot be determined.
#!/usr/bin/python3
# Import module
import platform
print(platform.node())
installmd.com
Using uname Method
You will probably load the os module anyway, so another suggestion would be:
#!/usr/bin/python3
# Import module
import os
print(os.uname()[1])
print(os.uname())
installmd.com posix.uname_result(sysname='Linux', nodename='installmd.com', release='3.10.0-1160.el7.x86_64', version='#1 SMP Mon Oct 19 16:18:59 UTC 2020', machine='x86_64')