0

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.

2 Answers2

0

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)
Donovan Solms
  • 941
  • 12
  • 17
  • Thanks a lot. Perfect simple answer. What would the urllib request be in python 4.3.2? –  Feb 10 '15 at 18:57
  • [Fetching URL's in Python 3](https://docs.python.org/3/howto/urllib2.html) should give you what it needs to be in Python 3 – Donovan Solms Feb 10 '15 at 19:07
0

You can take a look at userinfo.io.

Issuing a GET http://api.userinfo.io/userinfos will return you all the information you're asking for. You can also specify the IP address in the parameters: GET http://api.userinfo.io/userinfos?ip_address=888.888.888.888.

The response looks like it:

{
  request_date: "2014-09-18T04:11:25.154Z",
  ip_address: "192.77.237.95",
  position: {
    latitude: 37.7758,
    longitude: -122.4128,
    accuracy: 3   // This is the accuracy radius, in kilometers
  },
  continent: {
    name: "North America",
    code: "NA",
  },
  country: {
    name: "United States",
    code: "US",
  },
  city: {
    name: "San Francisco",
    code: "94103"
  }
}
Vincent Durmont
  • 813
  • 8
  • 15