DEV Community

WhiteHat💫
WhiteHat💫

Posted on

Simple Python Program To Find IP Address

What is an Ip Address?
An Internet Protocol address (Ip Address) is an identifier assigned to each computer and other device e.g., router, mobile, etc connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in human readable format such as 192.168.55.10 in IPv4(32-bit IP address). Addresses in IPv4 are 32-bits long. This allows for a maximum of 4,294,967,296 (232) unique addresses. Addresses in IPv6 are 128-bits, which allows for 3.4 x 1038 (2128) unique addresses. In this article we will focus on how to get the Ip address of your computer in python. First of, you will have to import the python socket library and proceed to use this "IP=socket.gethostbyname(hostname)" then you print the value of the ip into the print() function in you code, the output of your IP address would be displayed as shown in the program below:

"""Python program to find Ip Address"""
import socket

hostname = socket.gethostname()

IPAddr = socket.gethostbyname(hostname)

print("Your Computer Name is:" + hostname)

print("Your Computer IP Address is:" + IPAddr)

OUTPUT:
Your Computer Name is: mycomputer
Your Computer IP Address is:192.168.40.109

Conclusion
With python, there are lots of programs you can write and explore, this is just one of many of them. Get hands on with this simple script and see your results. And that's it! If you have any suggestions or questions for improvements please let me know in the replies below 😁

Top comments (2)

Collapse
 
vimrichie profile image
Vimrichie

Networking and Python are my two favorite things. Thanks for sharing.

Collapse
 
jeremih profile image
WhiteHat💫

You are welcome.... :-)