Geolocation with GeoIP 2
IP geolocation is the mapping of an IP address to the geographic location of the internet from the connected device. By geographically mapping the IP address, it provides you with location information such as the country, state, city, zip code, latitude/longitude.
API Reference:
The GeoIP object does not require any parameters to use the default settings. However, at the very least the GEOIP_PATH setting should be set with the path of the location of your GeoIP datasets.
Keyword Arguments:
* Path:
Base directory to where GeoIP data is located or the full path to where the city or country data files (.mmdb) are located. Assumes that both the city and country datasets are located in this directory; overrides the GEOIP_PATH setting.
* Cache:
The cache settings when opening up the GeoIP datasets. May be an integer in (0, 1, 2, 4, 8) corresponding to the MODE_AUTO, MODE_MMAP_EXT, MODE_MMAP, and GEOIP_INDEX_CACHE MODE_MEMORY C API settings, respectively. Defaults to 0 (MODE_AUTO).
* Country:
The name of the GeoIP country data file. Defaults to GeoLite2-Country.mmdb. Setting this keyword overrides the GEOIP_COUNTRY setting.
* City:
The name of the GeoIP city data file. Defaults to GeoLite2-City.mmdb. Setting this keyword overrides the GEOIP_CITY setting.
Methods:
Instantiating:
classmethod GeoIP2.open(path, cache)
Querying:
GeoIP2.city(query)
Returns a dictionary of city information for the given query. Some of the values in the dictionary may be undefined (None).
GeoIP2.country(query)
Returns a dictionary with the country code and country for the given query.
GeoIP2.country_code(query)
Returns the country code corresponding to the query.
GeoIP2.country_name(query)
Returns the country name corresponding to the query.
Coordinate Retrieval:
GeoIP2.coords(query)
Returns a coordinate tuple of (longitude, latitude).
GeoIP2.lon_lat(query)
Returns a coordinate tuple of (longitude, latitude).
GeoIP2.lat_lon(query)
Returns a coordinate tuple of (latitude, longitude),
GeoIP2.geos(query)
Returns a Point object corresponding to the query.
Which database to use?
Overall, all geolocation databases have room to improve their router geolocation accuracy at both country- and city-level. Researchers and network operators need to be aware of inaccuracies and their impact on their results.
That said, if you intend to use one of the geolocation databases we tested, here are our recommendations:
NetAcuity-
we recommend using this to geolocate routers if using a geolocation database is the only available option. NetAcuity has the best combination of coverage and accuracy across all regions.
MaxMind-
we recommend using the commercial version of MaxMind over the public version if city-level accuracy and better coverage are required. That said, we don't recommend MaxMind databases if high city-level accuracy and coverage are required the city-level accuracy is especially bad in the ARIN region, however, we do see relatively good city-level results for MaxMind in the RIPE NCC and APNIC regions.
IP2Location-Lite - we don't recommend this service given the overall accuracy is too low.
Comments