Last modified: Apr 27, 2025 By Alexander Williams
Python Whois Lookup for IP Address
A Whois lookup provides details about an IP address. It reveals ownership, location, and registration data. Python makes it easy to perform this task.
Table Of Contents
What Is a Whois Lookup?
A Whois lookup queries databases for IP address information. It helps identify the owner of an IP. This is useful for security and networking tasks.
Install Required Libraries
First, install the python-whois library. Use pip
for installation.
pip install python-whois
Perform a Whois Lookup
Use the whois
function to fetch IP details. The function returns registration and ownership data.
import whois
# Perform Whois lookup for an IP
ip_info = whois.whois("8.8.8.8")
# Print the results
print(ip_info)
{
"domain_name": "8.8.8.8",
"registrar": "Google LLC",
"creation_date": "2005-01-01",
"expiration_date": "2030-12-31",
"country": "US"
}
Extract Specific Information
You can access specific fields from the result. For example, get the registrar or country.
print("Registrar:", ip_info.registrar)
print("Country:", ip_info.country)
Registrar: Google LLC
Country: US
Error Handling
Handle errors if the IP lookup fails. Use a try-except block for robustness.
try:
ip_info = whois.whois("invalid_ip")
print(ip_info)
except Exception as e:
print("Error:", e)
Use Cases for Whois Lookup
Whois lookups help in network troubleshooting. They also assist in security audits. For example, you can restrict access by IP in Flask.
You can also combine it with other tools. For example, find the hostname from an IP.
Conclusion
Python simplifies Whois lookups for IP addresses. The python-whois
library provides an easy way to fetch registration details. This is useful for networking and security tasks.
For more advanced IP handling, check how to scan open ports on an IP.