Question

I have GeoIP city and region codes like so:

region_code:H9
country_code:GB

The city and region codes I have were obtained via GeoIP from IP address at some point in the past. However, IP address has not been retained. I believe it should be possible to index into the GeoLite City database and retrieve latitude and longtitude with this info but I can't see anything out of the box in the pygeoip wrapper to do so. Does anyone know how to do this?

Was it helpful?

Solution

GeoIP City provides a mapping from IP address to location (including coordinates). For instance:

>>> gi = pygeoip.GeoIP('GeoIPCity.dat')
>>> gi.record_by_addr('64.233.161.99')
{
    'city': u'Mountain View',
    'region_code': u'CA',
    'area_code': 650,
    'time_zone': 'America/Los_Angeles',
    'dma_code': 807,
    'metro_code': 'San Francisco, CA',
    'country_code3': 'USA',
    'latitude': 37.41919999999999,
    'postal_code': u'94043',
    'longitude': -122.0574,
    'country_code': 'US',
    'country_name': 'United States',
    'continent': 'NA'
}
>>> gi.time_zone_by_addr('64.233.161.99')
'America/Los_Angeles'

However, it does not provide a mapping from region code to coordinates. For this, I would recommend using GeoNames.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top