In this article, you’ll learn how to find the IP address using python
- Python Basics
- 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:
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:
socket is an in-built module, hence is no need to install it separately.
''' 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 = "github.com" # 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: 192.168.0.105 The IP Address of github.com is: 184.108.40.206Code language: CSS (css)