Report this

What is the reason for this report?

Python - Get IP Address from Hostname

Published on August 4, 2022
Python - Get IP Address from Hostname

Python socket module can be used to get the IP address from a hostname.

The socket module is part of the Python core libraries, so we don’t need to install it separately.

Python Socket Module to Get IP Address from Hostname

Python socket module gethostbyname() function accepts hostname argument and returns the IP address in the string format.

Here is a simple example in the Python interpreter to find out the IP address of some of the websites.

# python3.7
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import socket
>>> socket.gethostbyname('journaldev.com')
'45.79.77.230'
>>> socket.gethostbyname('google.com')
'172.217.166.110'
>>> 

Note: If the website is behind a load-balancer or working in the cloud, you might get a different result for the IP address lookup.

For example, try to run the above command for google.com or facebook.com. If you are not in the same location as mine (India), chances are that you will get a different IP address as output.

Python Script to Find Out the IP Address of a Website

Let’s look at an example where we ask user to enter a website address and then print its IP address.

import socket

hostname = input("Please enter website address:\n")

# IP lookup from hostname
print(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')
Python Get Ip Address Hostname
Python Get Ip Address from Hostname

Here is another example to pass the hostname as a command-line argument to the script. The script will find the IP address and print it.

import socket
import sys

# no error handling is done here, excuse me for that
hostname = sys.argv[1]

# IP lookup from hostname
print(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')

Output:

# python3.7 ip_address.py facebook.com
The facebook.com IP Address is 157.240.23.35

Error Scenarios with socket.gethostbyname()

If the hostname doesn’t resolve to a valid IP address, socket.gaierror is raised. We can catch this error in our program using try-except block.

Here is the updated script with exception handling for invalid hostname.

import socket
import sys

hostname = sys.argv[1]

# IP lookup from hostname
try:
    ip = socket.gethostbyname(hostname)
    print(f'The {hostname} IP Address is {ip}')
except socket.gaierror as e:
    print(f'Invalid hostname, error raised is {e}')

Output:

# python3.7 ip_address.py jasjdkks.com               
Invalid hostname, error raised is [Errno 8] nodename nor servname provided, or not known
#

Reference: Socket Module API Docs

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?
Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.