Get Hostname in Python

Method 1: Using the Platform module

The Platform module is used to retrieve as much possible information about the platform on which the program is being currently executed. platform.node() function returns a string that displays the information about the node basically the system’s network name.

Python3




import platform
 
print(platform.node())


Output: 

GFGNDASM444

Method 2: Using Socket module

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The gethostname function retrieves the standard host name for the local computer.

Python3




import socket
 
print(socket.gethostname())


Output: 

GFGNDASM444

Display Hostname and IP address in Python

There are many ways to find the hostname and IP address of a local machine. Here is a simple method to find the hostname and IP address using python code. 
Library used – socket: This module provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, macOS, and probably additional platforms in Python

Similar Reads

Get Hostname in Python

Method 1: Using the Platform module...

Get IP address in Python

...

Display Hostname and IP address in Python

...