Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Display Hostname and IP Address in 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.

Socket. gethostname()
Socket. gethostbyname()

Algorithm

Step 1: use module socket.
Step 2: use gethostname() retrives the standard host name for the local machine.
Step 3: use gethostbyname() retrives host information corresponding to a host name from a host database.

Example Code

# Display hostname andIP address
import socket
def host_IP():
   try:
      hname = socket.gethostname()
      hip = socket.gethostbyname(hname)
      print("Hostname:  ",hname)
      print("IP Address: ",hip)
   except:
      print("Unable to get Hostname and IP")
# Driver code
host_IP() #Function call

Output

Hostname:   Satyajit-PC
IP Address:  192.168.1.66
Updated on: 2019-07-30T22:30:23+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements