In this article, you’ll learn how to find the IP address using python
- Python Basics
- socket module
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.
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: 126.96.36.199Code language: CSS (css)