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,,, etc. Let’s look at the following example.

import socket

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.

Also read:

One response to “How to get IP address of a URL in Python”

  1. Ramesh Mathad says:

    At line:1 char:2
    + & c:/anaconda/python.exe c:/FirewallProjects/tests/
    + ~
    The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks (“&”) to pass it as part of a string.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

Leave a Reply

Your email address will not be published. Required fields are marked *