Python Get Hostname

Python Get Hostname

Vaibhhav Khetarpal Mar-29, 2022 May-09, 2021 Python Python Network
  1. Use the gethostname() Method to Find the Hostname of a Machine in Python
  2. Use the platform Module to Find the Hostname of a Machine in Python
  3. Use the os.uname() Function to Find the Hostname of a Machine in Python

A hostname is a unique name or an alias assigned to a device or network node in a web of computer networks. It is mainly used to differentiate between the devices within a local network. They are explicit names or character strings that allude to a host and make it usable for the network and individuals.

This tutorial will introduce how to find the hostname in Python.

Use the gethostname() Method to Find the Hostname of a Machine in Python

The gethostname() function is used to return a string containing the machine’s hostname value on which the Python interpreter is currently executing.

To use the gethostname() function, the socket module needs to be imported to the python code.

The socket module in Python gives the programmer access to the BSD socket interface. The socket module provides several functions and operations that deal with creating full-fledged network applications that include client and server programs.

The following code uses the gethostname() function to get the hostname in Python.

import socket
print(socket.gethostname())

Use the platform Module to Find the Hostname of a Machine in Python

The platform module is an in-built module defined in Python that is used to find the information about the system on which the Interpreter is running.

This module assumes an essential part when there is a need to check whether your program is compatible with the current version of Python installed on your system or whether the hardware specifications meet the necessities of your program. The platform module already exists in the list of libraries in Python and does not need to be manually installed using pip.

The platform.node() function is used to retrieve the computer’s hostname.

The following code uses the platform module to get the hostname in Python.

import platform
print (platform.node())

Use the os.uname() Function to Find the Hostname of a Machine in Python

To use the os.uname() function, the os module needs to be imported to the Python code.

The OS module in Python can provide functions for a fluent interaction with the operating system. The OS module comes directly under Python’s standard utility modules. This module gives a convenient and portable method of utilizing operating system-reliant functionality.

The os.uname() function retrieves information about the system’s current operating system. The os.uname() further consists of several fields. The nodename field yields the machine’s name on the current network, which can be used to identify the hostname.

The following code uses the os.uname() function to get the hostname in Python.

import os
hname = os.uname()
print(hname)

The output of this code contains all the five attributes, and the nodename field yields the hostname in Python.

Some of the Python Interpreters might truncate the output of nodename to 8, and socket.gethostname() might sometimes give a more accurate output; therefore, it is a more consistent way to get the hostname.

Related Article - Python Network

  • Get IP Addresses in Python
  • Python Ping