Skip to main content

GeoIP2

This class provides an interface for querying GeoIP2 datasets to retrieve geographical information based on IP addresses or fully qualified domain names. It supports both city and country databases with various memory caching modes for optimized performance. Users can retrieve detailed location metadata, including coordinates, postal codes, and regional information, as well as GEOS Point objects.

Attributes

AttributeTypeDescription
MODE_AUTOint = 0Try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order.
MODE_MMAP_EXTint = 1Use the C extension with memory map.
MODE_MMAPint = 2Read from memory map. Pure Python.
MODE_FILEint = 4Read database as standard file. Pure Python.
MODE_MEMORYint = 8Load database into memory. Pure Python.
cache_optionsfrozenset = frozenset((0, 1, 2, 4, 8))A frozen set of valid memory caching flags used to validate the cache setting during initialization.

Constructor

Signature

def GeoIP2(
path: string = None,
cache: int = 0,
country: string = None,
city: string = None
) - > null

Parameters

NameTypeDescription
pathstring = NoneBase directory or full path to the city or country .mmdb data files. Overrides the GEOIP_PATH setting.
cacheint = 0The cache settings for opening datasets (0, 1, 2, 4, or 8). Defaults to MODE_AUTO (0).
countrystring = NoneThe name of the GeoIP country data file. Defaults to 'GeoLite2-Country.mmdb'.
citystring = NoneThe name of the GeoIP city data file. Defaults to 'GeoLite2-City.mmdb'.

Signature

def GeoIP2(
path: string = None,
cache: int = 0,
country: string = None,
city: string = None
)

Parameters

NameTypeDescription
pathstring = NoneBase directory to where GeoIP data is located or the full path to where the city or country data files (*.mmdb) are located.
cacheint = 0The cache settings when opening up the GeoIP datasets, corresponding to MODE_AUTO, MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, or MODE_MEMORY.
countrystring = NoneThe name of the GeoIP country data file, defaulting to 'GeoLite2-Country.mmdb'.
citystring = NoneThe name of the GeoIP city data file, defaulting to 'GeoLite2-City.mmdb'.

Methods


is_city()

@classmethod
def is_city() - > boolean

Determines if the loaded database contains city-level geolocation data.

Returns

TypeDescription
booleanTrue if the database type includes 'City', False otherwise.

is_country()

@classmethod
def is_country() - > boolean

Determines if the loaded database contains country-level geolocation data.

Returns

TypeDescription
booleanTrue if the database type includes 'Country', False otherwise.

city()

@classmethod
def city(
query: string|IPv4Address|IPv6Address
) - > dict

Return a dictionary of city information for the given IP address or Fully Qualified Domain Name (FQDN). Some information in the dictionary may be undefined (None).

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
dictA dictionary containing city, region, country, and coordinate data.

country_code()

@classmethod
def country_code(
query: string|IPv4Address|IPv6Address
) - > string

Return the country code for the given IP Address or FQDN.

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
stringThe ISO country code associated with the query.

country_name()

@classmethod
def country_name(
query: string|IPv4Address|IPv6Address
) - > string

Return the country name for the given IP Address or FQDN.

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
stringThe full name of the country associated with the query.

country()

@classmethod
def country(
query: string|IPv4Address|IPv6Address
) - > dict

Return a dictionary with the country code and name when given an IP address or a Fully Qualified Domain Name (FQDN). For example, both '24.124.1.80' and 'djangoproject.com' are valid parameters.

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
dictA dictionary containing continent and country codes and names.

lon_lat()

@classmethod
def lon_lat(
query: string|IPv4Address|IPv6Address
) - > tuple

Return a tuple of the (longitude, latitude) for the given query.

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
tupleA tuple containing the longitude and latitude as floats.

lat_lon()

@classmethod
def lat_lon(
query: string|IPv4Address|IPv6Address
) - > tuple

Return a tuple of the (latitude, longitude) for the given query.

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
tupleA tuple containing the latitude and longitude as floats.

geos()

@classmethod
def geos(
query: string|IPv4Address|IPv6Address
) - > [Point](../geos/point/point.md?sid=django_contrib_gis_geos_point_point)

Return a GEOS Point object for the given query.

Parameters

NameTypeDescription
query`stringIPv4Address

Returns

TypeDescription
[Point](../geos/point/point.md?sid=django_contrib_gis_geos_point_point)A Django GEOS Point object representing the location with SRID 4326.