Python Program to Get IP Address

« Previous Program Next Program »

Get IP Address in Python

To get IP address of your computer in python, you have to first import socket library and then use socket.gethostbyname(socket.gethostname()) code inside the print() statement to print your IP address as output as shown in the program given below.

Python Programming Code to Get IP Address

Following python program prints IP address of your computer:

# Python Program - Get IP Address

import socket;
print("Want to get IP Address ? (y/n): ");
check = input();
if check == 'n':
    print("\nYour IP Address is: ",end="");

Here is the sample run of the above python program to illustrate how to get and print IP address of your computer:

python get ip address of computer

Now supply y as input and press enter key to see the IP address of your computer system:

python program get ip address

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test