Last modified: Apr 27, 2025 By Alexander Williams

Find Hostname from IP Address in Python

Finding a hostname from an IP address is a common task in networking. Python makes it easy with the socket module. This guide will show you how.

What Is a Hostname?

A hostname is a label assigned to a device on a network. It helps identify the device. For example, "google.com" is a hostname.

An IP address is a numerical label. It is used to locate devices on a network. Converting an IP to a hostname is called reverse DNS lookup.

Using Python's Socket Module

The socket module in Python provides low-level networking functions. It includes methods to perform reverse DNS lookups.

Here is a simple example:


import socket

ip_address = "8.8.8.8"  # Google's public DNS IP
hostname = socket.gethostbyaddr(ip_address)
print(f"Hostname: {hostname[0]}")


Hostname: dns.google

The gethostbyaddr method returns a tuple. The first element is the hostname. The other elements are aliases and IP addresses.

Handling Errors

Sometimes, the IP address may not resolve to a hostname. In such cases, the method raises an exception. Here is how to handle it:


import socket

ip_address = "192.168.1.1"  # Example local IP
try:
    hostname = socket.gethostbyaddr(ip_address)
    print(f"Hostname: {hostname[0]}")
except socket.herror:
    print("Hostname not found")


Hostname not found

This ensures your program does not crash. It gracefully handles errors.

Using Multiple IP Addresses

You can also resolve multiple IP addresses. Loop through a list of IPs and get their hostnames.


import socket

ips = ["8.8.8.8", "8.8.4.4", "1.1.1.1"]  # Example IPs
for ip in ips:
    try:
        hostname = socket.gethostbyaddr(ip)
        print(f"IP: {ip} -> Hostname: {hostname[0]}")
    except socket.herror:
        print(f"IP: {ip} -> Hostname not found")


IP: 8.8.8.8 -> Hostname: dns.google
IP: 8.8.4.4 -> Hostname: dns.google
IP: 1.1.1.1 -> Hostname: one.one.one.one

This is useful for bulk operations. It saves time and effort.

Practical Applications

Finding hostnames from IPs has many uses. It helps in network troubleshooting. It also aids in security analysis.

For example, you can scan open ports on an IP address. Then, resolve the IP to a hostname for better identification.

You can also get all IP addresses on a local network. Then, find their hostnames for better management.

Performance Considerations

Reverse DNS lookups can be slow. They depend on network conditions and DNS servers. Always handle timeouts.

Here is an example with a timeout:


import socket

socket.setdefaulttimeout(5)  # 5 seconds timeout
ip_address = "8.8.8.8"
try:
    hostname = socket.gethostbyaddr(ip_address)
    print(f"Hostname: {hostname[0]}")
except (socket.herror, socket.timeout):
    print("Hostname lookup failed")

This prevents your program from hanging. It ensures timely responses.

Conclusion

Finding a hostname from an IP in Python is simple. Use the socket.gethostbyaddr method. Handle errors and timeouts for robustness.

This skill is useful in networking and security tasks. For more, check our guide on validating IP addresses in Python.