Search code examples
pythongeolocationip-geolocation

What API, package or library is easiest to find the location of an IP address?


this is my scenario. I want to be able to write a script in python that when entered an IP address, it looks that ip address up on a API, and returns the result. Can anyone tell me a good working lookup API with the code. For example this is hostip:

import urllib

response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()

print(response)

However hostIP has a very small database, and cannot return many locations.


Solution

  • I've had great success with freegeoip.net. They provide a public (free) API, or you can run it on your own server.

    Simple code sample:

    import urllib
    response = urllib.urlopen('http://freegeoip.net/json/1.2.3.4').read()
    print(response)