Python program to find the IP address

Python program to find the IP address:

To find the IP address, we can use socket package of python. It has two methods, we can use to get the IP address.

  1. gethostname: It returns host name of the current machine
  2. gethostbyname(hostname): We can pass the hostname that returns by the above program to get the ip address.

Below is the complete program:

import socket 

host_name = socket.gethostname()
print('host name : {}'.format(host_name))

ip = socket.gethostbyname(host_name)
print('Your local ip : {}'.format(ip))

It will print the host name and the ip address where you are running this script.

You might also like: