Tutorials  /  Python

Python - Get IP Address from Hostname

Ccentron Redaktion · February 2024 ·3 min read ·Python, Tutorial

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.

Console
# 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.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Python Script to get IP Address from Hostname

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

Python
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

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.

Python
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:

Console
# 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.

Python
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:

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

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren