Python to Get IP Address and Hostname of Website with Full Source Code

Get the IP address and hostname of a website

Prerequisites:

None

Run the Script:

  • Execute
python3 Hostname_IPaddress.py
Code language: CSS (css)

Source Code:

Hostname_IPaddress.py

# importing socket library
import socket

def get_hostname_IP():
    hostname = input("Please enter website address(URL):")
    try:
        print (f'Hostname: {hostname}')
        print (f'IP: {socket.gethostbyname(hostname)}')
    except socket.gaierror as error:
        print (f'Invalid Hostname, error raised is {error}')

get_hostname_IP()

Output:

Leave a Comment