How to Get the IP Address of a Website in Python using the Socket Module


In this article, we show how to get the IP address of a website in Python using the socket module.

So if you know the hostname of a website, you can obtain the IP address for this website easily using the socket module in Python.

Hostnames are the names of websites, such as,,,, etc.

We humans understand hostnames very well. When we want to go to a certain website, we go to the address bar of the browser and type in or, etc.

We are then brought to the site.

Computers, however, function with the IP address and less care about hostnames than we do. Every time we type in a domain name such as,, the browser really doesn't understand this hostname. What browsers understand is the IP address. The IP address is the address of the server which houses the pages of a website. The browser then communicates with this server through the IP address with HTTP and other protocols and gets the requested page that a user has entered.

When a user enters a domain name, what the browser has is go to a domain name server and look up this domain name entered in and get the IP address of this domain name. It then goes to the computer that has this IP address and gets the requested page.

So let's find the IP address of this website,

This is shown in the following code below.

So the first thing we have to do is import the socket module, which we do with the line, import socket

We set the hostname equal to the string, ''

We then create another variable, hostname_ip, and set it equal to, socket.gethostbyname(hostname)

This variable now contains the IP address of the hostname,

This returns the IP address of

So this is the way we can get the IP address of a website in Python using the socket module.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...