Know IP Address of a website

This blogs describes how to find IP address for a website.

We require socket module and gethostbyname() function to find IP Address of a website. Function takes website name and returns IP address of a website.

In our code, we will ask user to enter website then using gethostbyname() function to find IP address. Function returns gaierror (Get Address Information Error) if it does not find website.

Step 1 Importing socket module

Step 2 Asking user to enter website or url

Step 3 Using gethostbyname() function to find IP address and printing it.

Step 4 Printing error message if website not found

Below is the code

import socket
host = input('Please enter website or url: ')
try:
    addr = socket.gethostbyname(host)
    print('IP Address:',addr)
except socket.gaierror:
    print('Website not found')

 

Output 1 (Website Found)

Please enter website or url: www.facebook.com
IP Address: 157.240.16.35

Output 2 (Website not found)

Please enter website or url: www.loddjjkdj.com
Website not found

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s