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 thi开发者_JAVA百科s 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.
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.
Ok, here is my answer. I am going to work on cleaning up for public consumption a Python 3.x version of pywhois that I have on my machine and hopefully in the next week I will submit my code to the subversion repository. From the IP addresses I am using, I have about a 78% success rate for retrieving info first by applying gethostbyaddr to the IP address as phihag suggested and then putting that through pywhois. I will let the reader decide for themselves whether that rate is high enough for their particular application.
精彩评论