How to get IP address of a URL in Python
In this article, we show you how to get the IP address of a URL or website in Python. In order to find the IP address of a URL or website, we can use the socket module that is available in Python. This tutorial will help you to learn about something new and useful.
Python program to get the IP address of a URL
First of all, you have to know about the IP address and URL. An Internet Protocol (IP) address is a unique string of numbers that identifies each computer using the internet protocol to communicate over a network. URL stands for Uniform Resource Locator. A URL is the address of a resource on the internet. Some of the examples for URL are www.google.com, www.codespeedy.com, www.facebook.com, etc. Let’s look at the following example.
import socket url="www.codespeedy.com" print("IP:",socket.gethostbyname(url))
In this example, we have imported socket module to get the IP address of a URL in Python. A URL is assigned to a variable. This variable acts as an argument for the gethostbyname() function. The gethostbyname() function takes hostname as an argument and returns the IP address of a host.
- Syntax: gethostbyname(hostname)
- hostname- The name of the host system for which IP address resolution is needed.
There are two versions of IP that currently coexist in the global internet. They are IP version 4 (IPV4) and IP version 6 (IPV6). IPV4 addresses are 32 bits long and IPV6 address is 128 bits long. In this program, the returned IP address is an IPV4 address. Let us look at the sample input and its corresponding output.
I hope that you have learned something useful from this tutorial.