An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
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
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To