Open In App
Related Articles

Python program to find IP Address

Improve Article
Save Article
Like Article

An IP(Internet Protocol) 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 notation such as in IPv4(32-bit IP address).

Get IP address of your computer in Python

Here we are trying to fetch the IP address of our computer in Python using various methods like,

Using the socket library to find IP Address

Step 1: Import socket library


IP = socket.gethostbyname(hostname)

Step 2: Then print the value of the IP into the print() function your IP address.


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

Below is the complete Implementation

Here we have to import the socket first then we get the hostname by using the gethostname() function and then we fetch the IP address using the hostname that we fetched and the we simply print it.


# Python Program to Get IP Address
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)


Your Computer Name is:pppContainer
Your Computer IP Address is:

Using the os module to find IP Address

Here we are using the os module to find the system configuration which contains the IPv4 address as well.


import os



Using the request module to find IP Address

Here we are using the request module of Python to fetch the IP address of the system.


from urllib.request import urlopen
import re as r
def getIP():
    d = str(urlopen('')
    return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').


Python program to find IP Address with request module

To find the IP address using the requests module in Python, you can make an HTTP request to a website that returns your IP address. Here’s an example program that demonstrates this approach:


import requests
def get_ip_address():
    url = ''
    response = requests.get(url)
    ip_address = response.text
    return ip_address
# Call the function to get the IP address
ip = get_ip_address()
print("IP Address:", ip)


IP Address:

Related Post : Java program to find IP address of your computer

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 Jun, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials