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
| Attribute | Type | Description |
|---|
| MODE_AUTO | int = 0 | Try MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that order. |
| MODE_MMAP_EXT | int = 1 | Use the C extension with memory map. |
| MODE_MMAP | int = 2 | Read from memory map. Pure Python. |
| MODE_FILE | int = 4 | Read database as standard file. Pure Python. |
| MODE_MEMORY | int = 8 | Load database into memory. Pure Python. |
| cache_options | frozenset = 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
| Name | Type | Description |
|---|
| path | string = None | Base directory or full path to the city or country .mmdb data files. Overrides the GEOIP_PATH setting. |
| cache | int = 0 | The cache settings for opening datasets (0, 1, 2, 4, or 8). Defaults to MODE_AUTO (0). |
| country | string = None | The name of the GeoIP country data file. Defaults to 'GeoLite2-Country.mmdb'. |
| city | string = None | The 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
| Name | Type | Description |
|---|
| path | string = None | Base directory to where GeoIP data is located or the full path to where the city or country data files (*.mmdb) are located. |
| cache | int = 0 | The cache settings when opening up the GeoIP datasets, corresponding to MODE_AUTO, MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, or MODE_MEMORY. |
| country | string = None | The name of the GeoIP country data file, defaulting to 'GeoLite2-Country.mmdb'. |
| city | string = None | The 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
| Type | Description |
|---|
boolean | True 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
| Type | Description |
|---|
boolean | True 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
dict | A 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
dict | A 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
tuple | A 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
tuple | A 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
| Name | Type | Description |
|---|
| query | `string | IPv4Address |
Returns
| Type | Description |
|---|
[Point](../geos/point/point.md?sid=django_contrib_gis_geos_point_point) | A Django GEOS Point object representing the location with SRID 4326. |