Finding IP Address using Python Last Updated : 09 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 192.168.1.35 in IPv4(32-bit IP address).Using the socket library to find IP AddressStep 1: Import socket libraryIPAddr = 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 import socket hostname = socket.gethostname() IPAddr = socket.gethostbyname(hostname) print("Your Computer Name is:" + hostname) print("Your Computer IP Address is:" + IPAddr) OutputYour Computer Name is:pppContainerYour Computer IP Address is:10.98.162.168Explanation:socket.gethostname() retrieves your computer's name (hostname).socket.gethostbyname(hostname) gets the corresponding IP address of that hostname.Using the os module to find IP AddressHere we are using the os module to find the system configuration which contains the IPv4 address as well. Python import os print(os.system('ipconfig')) OutputIP AddressExplanation:os.system('ipconfig') runs the ipconfig command in the system shell (Windows only). This command displays network configuration, including IP addresses.os.system() returns the exit status code of the command, not its output.print(os.system(...)) will show the entire ipconfig output in the console and then print the exit code (usually 0 if successful).Using the request module to find IP AddressHere we are using the request module of Python to fetch the IP address of the system. Python from urllib.request import urlopen import re as r def getIP(): d = str(urlopen('http://checkip.dyndns.com/') .read()) return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)'). search(d).group(1) print(getIP()) Output103.251.142.122Explanation:This code sends an HTTP request to http://checkip.dyndns.com/, which returns a web page containing your public IP address.The response is a string like "Current IP Address: 203.0.113.45".A regular expression (re) is used to extract the IP address from the string. Finally, it prints the public IP address.Related Post: Java program to find IP address of your computer Comment More infoAdvertise with us Next Article Python program to validate an IP Address A ajay0007 Follow Improve Article Tags : Technical Scripter Python Python Programs Computer Networks-IP Addressing Practice Tags : python Similar Reads Find the Index of a Substring in Python Finding the position of a substring within a string is a common task in Python. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python.Using str.find() The find() method searches for the first occurrence of the specified substring and return 3 min read Python program to find the type of IP Address using Regex Prerequisite: Python Regex Given an IP address as input, write a Python program to find the type of IP address i.e. either IPv4 or IPv6. If the given is neither of them then print neither. What is an IP (Internet Protocol) Address?Every computer connected to the Internet is identified by a unique fo 2 min read How to Find Index of a Substring in Python In Python, sometimes we need to know where a certain substring appears in a string. To do this, we can find the index (or position) of that substring. The index tells us where the substring is located. Letâs look at some simple ways to find the index of a substring in a string.Using find() methodThe 2 min read Python Program to Find and Print Address of Variable In this article, we are going to see how to find and print the address of the Python variable. It can be done in these ways:Using id() functionUsing addressof() functionUsing hex() functionMethod 1: Find and Print Address of Variable using id()We can get an address using id() function, id() function 2 min read Python program to validate an IP Address Prerequisite: Python Regex Given an IP address as input, write a Python program to check whether the given IP Address is Valid or not. What is an IP (Internet Protocol) Address? Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) a 4 min read Python program to validate an IP Address Prerequisite: Python Regex Given an IP address as input, write a Python program to check whether the given IP Address is Valid or not. What is an IP (Internet Protocol) Address? Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) a 4 min read Like