codescracker


python

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
while True:
	print("Want to get IP Address ? (y/n): ")
	check = input()
	if check == 'n':
		break
	else:
		print("Your IP Address is: ",end="")
		print(socket.gethostbyname(socket.gethostname()))
		print()

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

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 »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test