1.9. geoip

This module uses GeoLite2 data created by MaxMind, available from http://www.maxmind.com.

1.9.1. Data

DB_RESULT_FIELDS[source]

A tuple listing the fields that are required in database results.

1.9.2. Functions

download_geolite2_city_db(dest, license=None, date=None)[source]

Download the GeoLite2 database and save it to disk.

Changed in version 1.16.0: Added the license and date parameters.

Parameters:
  • dest (str) – The file path to save the database to.
  • license (str) – The MaxMind license key to use to download the database.
  • date (datetime.date) – The date for which to download the database.
init_database(database_file)[source]

Create and initialize the GeoLite2 database engine. This must be done before classes and functions in this module attempt to look up results. If the specified database file does not exist, a new copy will be downloaded.

Parameters:database_file (str) – The GeoLite2 database file to use.
Returns:The initialized GeoLite2 database object.
Return type:geoip2.database.Reader
lookup(ip, lang='en')[source]

Lookup the geo location information for the specified IP from the configured GeoLite2 City database.

Parameters:
  • ip (str) – The IP address to look up the information for.
  • lang (str) – The language to prefer for regional names.
Returns:

The geo location information as a dict. The keys are the values of DB_RESULT_FIELDS.

Return type:

dict

1.9.3. Classes

class Coordinates(latitude, longitude)[source]

A named tuple for representing GPS coordinates.

latitude[source]

Alias for field number 0

longitude[source]

Alias for field number 1

class GeoLocation(ip, lang='en', result=None)[source]

The geographic location information for a given IP address. If result is not specified, lookup() will be used to obtain the information.

__geo_interface__[source]

A simple implementation of the Python __geo_interface__ specification. This allows this object to be used with modules which also support this interface such as geojson.

Returns:A dictionary describing a this location as a GeoJSON Point.
Return type:dict
__init__(ip, lang='en', result=None)[source]
Parameters:
  • ip (str) – The IP address to look up geographic location data for.
  • lang (str) – The language to prefer for regional names.
  • result (dict) – A raw query result from a previous call to lookup().
city[source]
continent[source]
coordinates[source]
country[source]
classmethod from_graphql(ip, result, lang='en')[source]
ip_address[source]

The IPv4Address which this geographic location data describes.

postal_code[source]
time_zone[source]