3

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.

demongolem
  • 9,474
  • 36
  • 90
  • 105

2 Answers2

5

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.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • "Note that the free "Lite" versions will give you only a rough idea, and errors of 20-50km are not uncommon" Hehehe, that is far far more accurate than I need. I was not planning on visiting anyone's house here :) I think option 1 will work better if it does what it claims and what I expect a whois program to do. I am checking out with svn as we speak. Thanks for the answer. – demongolem Jul 07 '11 at 13:43
  • I'll keep as best answer, but just to let the general public know, pywhois does not seem to play too well with Python 3.x. – demongolem Jul 08 '11 at 01:23
  • @demongolem Are you sure you need whois data in the first place? Your first comment sounded like all you need is the country. For that, IP-based geolocation is the way faster and better solution. – phihag Jul 08 '11 at 08:25
  • Well I'll get what I can take. If geolocation is the best I can get, so be it. Actually, my preferred solution is to edit pywhois myself to work in Python 3.x. Don't know if I have the time, but hey it is only 3 files and maybe someone else out there in time and/or space will find it useful. – demongolem Jul 08 '11 at 13:02
0

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.

demongolem
  • 9,474
  • 36
  • 90
  • 105