How to get hostname in Python?

ItsMyCode |

There are several ways to get a hostname in Python. The most popular and convenient way is to use the socket module, which provides the BSD socket interface. It’s available and works across all the different OS such as UNIX, Windows, Mac OS X, BSD, OS/2, etc.

We can even create a full-fledged network application in Python that includes client-server communications using the socket module.

Python code to get a hostname using the socket module

Python socket module has a function named gethostname(), using which we can easily find the hostname of a given machine.

Syntax – socket.gethostname()

The gethostname()doesn’t accept any parameters, but it returns the current hostname of the machine in string format.

*Example – *

# import the socket module in Python
import socket

# Print the hostname of the given system using gethostname() method 
print("The hostname of the current system is ",socket.gethostname())

Output

The hostname of the current system is ItsMyCode

Python code to find a hostname using the platform module

Platform module is another popular module in Python that comes built-in with Python installation. It is used to access the platform’s data such as networking, operating system, interpreter version, etc.

Syntax – platform.node()

The platform.node() function doesn’t accept any parameters, but it returns the current hostname of the machine in string format.

*Example – *

# import the platform module in Python
import platform

# Print the hostname of the given system using platform.node() method 
print("The hostname of the current system is ",platform.node())

Output

The hostname of the current system is ItsMyCode

Python code to find a hostname using the os module

The OS module in Python is mainly used to interact with the operating system, and it comes as the built-in utility module with Python installation.

There are two ways to find a hostname using the OS module

Using os.uname()

Syntax – os.uname()

Returns information identifying the current operating system. The return value is an object with five attributes:

  • sysname – operating system name
  • nodename – the name of the machine on the network (implementation-defined)
  • release – operating system release
  • version – operating system version
  • machine – hardware identifier

Example –

# import the os module in Python
import os

# Print the current system details using os.uname() method 
print("The current system details are is ", os.uname())

# Print the hostname of the given system using os.uname() method 
print("The current system's hostname is ", os.uname()[1])

Output

('The current system details are is ', ('Linux', 'd5624dfa0f42', '3.10.0-1160.25.1.el7.x86_64', '#1 SMP Wed Apr 28 21:49:45 UTC 2021', 'x86_64'))

('The current system hostname is ', 'd5624dfa0f42')

Note – The os.uname() method is supported in only a few operating systems, and if you get AttributeError: module ‘os’ has no attribute ‘uname’, ** then try the below approach **os.getenv()

Using os.getenv()

Syntax – os.getenv(key, default=None)

Return the value of the environment variable key if it exists, or default if it doesn’t exist.

*Example – *

# import the os module in Python
import os

# Print the hostname of the given system using os.getenv() method 
print("The hostname of the current system is ",os.getenv('COMPUTERNAME', 'defaultValue'))

Output

The hostname of the current system is ItsMyCode

Note – os.getenv('HOSTNAME') don’t always work in cron jobs as WSDL, HTTP HOSTNAME isn’t set. So better to use the socket way in order to get the hostname of a given system.

The post How to get hostname in Python? appeared first on ItsMyCode.

17