ipwhois is a Python package designed for retrieving and parsing whois data for both IPv4 and IPv6 addresses. It is currently at version 1.3.0 and actively maintained, though its release cadence can be irregular.
pip install ipwhoisVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to perform a basic RDAP lookup using the IPWhois class. RDAP (Registration Data Access Protocol) is the recommended method for retrieving data. The `depth` parameter controls how many recursive lookups are performed for associated entities.
Migrate to `IPWhois.lookup_whois()` for legacy WHOIS queries or, preferably, `IPWhois.lookup_rdap()` for RDAP queries, which offers a better data structure.
Refer to the v1.2.0 changelog and upgrade notes for a full list of removed components and their replacements. For ASN lookups, refer to the `IPASN` class and its current methods.
Upgrade `ipwhois` to version 1.3.0 or later to ensure compatibility with recent `dnspython` versions.
Implement proper caching mechanisms for WHOIS data, introduce delays between queries, or consider using experimental bulk query support (with caution) if applicable.
If experiencing performance or rate-limiting issues, set `root_ent_check=False` in `lookup_rdap()` to revert to the older functionality with fewer queries (but potentially less data).
Install the package using pip: `pip install ipwhois` or `pip3 install ipwhois` if you have multiple Python versions.
Update your code to use the currently supported methods, `lookup_rdap()` or `lookup_whois()`: `results = obj.lookup_rdap()` or `results = obj.lookup_whois()`.
Implement error handling for `WhoisLookupError` to manage lookup failures gracefully. Check network connectivity, verify the IP address, and consider increasing the timeout parameter for the `IPWhois` object or the lookup method. Example: `try: obj.lookup_rdap(timeout=10) except WhoisLookupError: # handle error`.
Access dictionary keys safely using `dict.get()` with a default value, or check for key existence before access. Example: `country = results.get('asn_country_code', 'N/A')` or `if 'network' in results and 'name' in results['network']: network_name = results['network']['name']`.