Search code examples
pythonreverse-dnsgethostbyaddrpywhois

In Python, Getting More Info About An IP Address


I know about gethostbyaddr in Python and that is somewhat useful for me. I would like to get even more info about an ip address like one can find at various websites such as who hosts that ip address, the country of origin, ..., etc. I need to accomplish this programmatically.

Are there any built in commands for Python, or would I need access to some database which contains this type of information, or are there any Python APIs? Python is not my native language so I am not as familiar with how one would approach such a problem in Python.


Solution

  • You can use pywhois for retrieving whois information, i.e. name and contact details of the hosting organization/person. Note that whois information quality varies; usually, the domain name (found with gethostbyaddr) will get you way better results than the IP address.

    If you need only geographic information, use pygeoip. It maps an IP address to a location by looking it up in a database, such as the ones provided by MaxMind. Note that the free "Lite" versions will give you only a rough idea, and errors of 20-50km are not uncommon.