Hey coder! In this tutorial, we will learn how to get the IP address of any website using the Python programming language.
In order to find the IP address of a website using the
socket module available in Python. This tutorial will help you to learn about something new and useful.
Also Read: Wikipedia Scraping In Python – Easy Ways
Understanding IP Address and their usage
It is a unique address that helps to identify a website on the internet through Internet Protocol. IP is just a set of rules governing the format of data sent over the internet. It helps in connecting our system to other devices all over the world.
An IP address is a series of numbers separated by periods. Each number in the address range from 0 to 255. This implies that the full address ranges from 0.0.0.0 to 255.255.255.255.
To know more about them, you can refer to the Wikipedia Page.
First, we import the
socket library to the code.
import socket as s
Now that we have the socket module in our python program, let’s start by fetching our own hostname using the code below.
my_host_name = s.gethostname() print('Your Hostname is: ' + my_host_name)
Your Hostname is: NightPsycho_04
Now, It’s time to fetch the IP Address for your own system using the
gethostbyname function as shown in the code below.
my_ip = s.gethostbyname(my_host_name) print('Your Ip Address is: ' + my_ip)
Your Ip Address is: 192.168.56.1
Next, we can use the same
gethostbyname function to find the IP address of the website. Let’s test with our own website: journaldev.com.
host ="journaldev.com" JD_ip_add = s.gethostbyname(host) print('IP Address for JournalDev is: ' + JD_ip_add)
IP Address for JournalDev is: 220.127.116.11
Let us try for another website as well just to make sure everything is working perfectly. We will consider askpython.com as a sample website for us.
host ="askpython.com" AP_ip_add = s.gethostbyname(host) print('IP Address for AskPython is: ' + AP_ip_add)
IP Address for AskPython is: 18.104.22.168
Congratulations! By following the steps above, you can successfully get the IP Address of any website using python. Hope you were able to learn something new through this tutorial!
Thank you for reading! Happy coding! 😄