Python to Find IP Address with Source Code

In this article, you’ll learn how to find the IP address using python


  1. Python Basics
  2. socket module

What is IP address?

IP address stands for Internet Protocol address, is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication.

IP (Internet Protocol) Address is an address of your network hardware. An IP Address is made up of numbers or characters. All devices that are connected to an internet connection have a unique IP address

There are two IP versions:

  • IPv4
  • IPv6

There are a few other types of IP addresses as well like private IP addresses, public IP addresses, static IP addresses and dynamic IP addresses.

Install Necessary Modules:

Since socket is an in-built module, hence is no need to install it separately.

Source Code:

Python Program to Find IP Address

# Import the necessary module!
import socket as s 

# Get my hostname
my_hostname = s.gethostname()
# Display my hostname
print("Your Hostname is: " + my_hostname)

# Get my IP
my_ip = s.gethostbyname(my_hostname)
# Display my IP 
print("Your Ip Address is: " + my_ip)

# Set the hostname
host = ""
# Fetch the IP
ip = s.gethostbyname(host)

# Display the IP
print("The IP Address of " + host + " is: "  + ip)


Your Hostname is: PARMAR-DESKTOP
Your Ip Address is:
The IP Address of is: language: CSS (css)

Leave a Comment