Nishant Gour
Nishant Gour's Blog


Nishant Gour's Blog

Display Hostname and IP address using Python

Display Hostname and IP address using Python

Nishant Gour's photo
Nishant Gour
ยทMay 26, 2021ยท

1 min read

Play this article

In this article, We are going to see how to Display the hostname and the IP address with the help of the language Python.

As we all know about IP, IP (Internet Protocol) is a fundamental networking concept that provides address assignation capability in a network.

How we are going to do it?. so we will be using the socket module of python

Python provides gethostname(),gethostbyname() two function. gethostname() retrives the standard host name for the local machine. gethostbyname() retrives host information corresponding to a host name from a host database.


import socket
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")

let's discuss the above 5 lines of code..

  • In our first line, we have imported our socket module.
  • in second-line we are getting the hostname by socket.gethostname() method.
  • The third line we retrives host information corresponding to a hostname from a host database.
  • and then the last two lines are for printing them.

So this is a short and sweet article for all of you!

I hope you liked it!. Let's catch up in the next articles.

If you liked it, Please Support Me