1

What library should I use to geocode by IP address in Python? Is there something similar to http://www.rubygeocoder.com/ available for python? I am having a hard time finding anything that does geocoding by IP.

Victor S
  • 4,990
  • 5
  • 42
  • 62
  • 2
    possible duplicate of [What python libraries can tell me approximate location and timezone given an IP address?](http://stackoverflow.com/questions/2543018/what-python-libraries-can-tell-me-approximate-location-and-timezone-given-an-ip) – jterrace Jan 17 '12 at 20:58

3 Answers3

4

Taken from this answer:

>>> import urllib
>>> response = urllib.urlopen(
     'http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()
>>> print response
Country: UNITED STATES (US)
City: Aurora, TX

Latitude: 33.0582
Longitude: -97.5159
IP: 12.215.42.19
Community
  • 1
  • 1
jterrace
  • 61,216
  • 21
  • 150
  • 193
3

Using Geocoder on GitHub/PyPi, you can get a full JSON object response from a specific IP address very simply by doing the following.

>>> import geocoder
>>> g = geocoder.ip('12.215.42.19')
>>> g.latlng
(38.0, -97.0)
>>> g.json

You can also search your current IP address by simply adding 'me' in the search field

g = geocoder.ip('me')
g.address

Install it by:

pip install geocoder

Different providers might be more accurate then others, but it's functional!

user1116928
  • 149
  • 2
0

Try the GeoIP() object:

https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoip/

Sid
  • 7,315
  • 2
  • 26
  • 41
  • I saw that, I was wondering if I don't have to commit to some arcane installation process... – Victor S Jan 17 '12 at 21:15
  • The installation should be pretty straight forward: http://www.maxmind.com/app/python They seem to have match.com,about.com as their clients. The only thing to investigate is the cost of a multi-site license if you need that. – Sid Jan 17 '12 at 21:29