Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 26, 2021 08:49 am GMT

Display Hostname and IP address using Python

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.

Code:

import sockethostname = 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.

original published on : https://codingnuts.tech/

If you liked it, Please Support Me


Original Link: https://dev.to/nishantgour/display-hostname-and-ip-address-using-python-5hng

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To