2015-07-29 21:21:03 +00:00
|
|
|
=======================
|
|
|
|
Geolocation with GeoIP2
|
|
|
|
=======================
|
|
|
|
|
|
|
|
.. module:: django.contrib.gis.geoip2
|
2016-02-19 07:31:25 +00:00
|
|
|
:synopsis: Python interface for MaxMind's GeoIP2 databases.
|
2015-07-29 21:21:03 +00:00
|
|
|
|
|
|
|
The :class:`GeoIP2` object is a wrapper for the `MaxMind geoip2 Python
|
|
|
|
library`__. [#]_
|
|
|
|
|
|
|
|
In order to perform IP-based geolocation, the :class:`GeoIP2` object requires
|
2023-04-16 23:14:09 +00:00
|
|
|
the :pypi:`geoip2` Python package and the GeoIP ``Country`` and/or ``City``
|
2022-07-29 16:34:18 +00:00
|
|
|
datasets in binary format (the CSV files will not work!), downloaded from e.g.
|
|
|
|
`MaxMind`__ or `DB-IP`__ websites. Grab the ``GeoLite2-Country.mmdb.gz`` and
|
|
|
|
``GeoLite2-City.mmdb.gz`` files and unzip them in a directory corresponding to
|
|
|
|
the :setting:`GEOIP_PATH` setting.
|
2015-07-29 21:21:03 +00:00
|
|
|
|
|
|
|
Additionally, it is recommended to install the `libmaxminddb C library`__, so
|
|
|
|
that ``geoip2`` can leverage the C library's faster speed.
|
|
|
|
|
2016-04-28 14:09:57 +00:00
|
|
|
__ https://geoip2.readthedocs.io/
|
2021-09-21 05:04:33 +00:00
|
|
|
__ https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
|
2022-07-29 16:34:18 +00:00
|
|
|
__ https://db-ip.com/db/lite.php
|
2021-04-27 11:09:00 +00:00
|
|
|
__ https://github.com/maxmind/libmaxminddb/
|
2015-07-29 21:21:03 +00:00
|
|
|
|
|
|
|
Example
|
|
|
|
=======
|
|
|
|
|
|
|
|
Here is an example of its usage:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2015-07-29 21:21:03 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2016-02-19 07:31:25 +00:00
|
|
|
>>> from django.contrib.gis.geoip2 import GeoIP2
|
|
|
|
>>> g = GeoIP2()
|
|
|
|
>>> g.country("google.com")
|
2024-01-10 11:09:44 +00:00
|
|
|
{'continent_code': 'NA',
|
|
|
|
'continent_name': 'North America',
|
|
|
|
'country_code': 'US',
|
|
|
|
'country_name': 'United States',
|
|
|
|
'is_in_european_union': False}
|
2016-02-19 07:31:25 +00:00
|
|
|
>>> g.city("72.14.207.99")
|
2024-01-10 11:09:44 +00:00
|
|
|
{'accuracy_radius': 1000,
|
|
|
|
'city': 'Mountain View',
|
|
|
|
'continent_code': 'NA',
|
|
|
|
'continent_name': 'North America',
|
|
|
|
'country_code': 'US',
|
|
|
|
'country_name': 'United States',
|
|
|
|
'is_in_european_union': False,
|
|
|
|
'latitude': 37.419200897216797,
|
|
|
|
'longitude': -122.05740356445312,
|
|
|
|
'metro_code': 807,
|
|
|
|
'postal_code': '94043',
|
|
|
|
'region_code': 'CA',
|
|
|
|
'region_name': 'California',
|
|
|
|
'time_zone': 'America/Los_Angeles',
|
|
|
|
'dma_code': 807,
|
|
|
|
'region': 'CA'}
|
2016-02-19 07:31:25 +00:00
|
|
|
>>> g.lat_lon("salon.com")
|
|
|
|
(39.0437, -77.4875)
|
|
|
|
>>> g.lon_lat("uh.edu")
|
|
|
|
(-95.4342, 29.834)
|
|
|
|
>>> g.geos("24.124.1.80").wkt
|
2016-04-16 18:05:47 +00:00
|
|
|
'POINT (-97 38)'
|
2015-07-29 21:21:03 +00:00
|
|
|
|
2017-08-23 10:11:49 +00:00
|
|
|
API Reference
|
2015-07-29 21:21:03 +00:00
|
|
|
=============
|
|
|
|
|
|
|
|
.. class:: GeoIP2(path=None, cache=0, country=None, city=None)
|
|
|
|
|
|
|
|
The ``GeoIP`` object does not require any parameters to use the default
|
|
|
|
settings. However, at the very least the :setting:`GEOIP_PATH` setting
|
|
|
|
should be set with the path of the location of your GeoIP datasets. The
|
|
|
|
following initialization keywords may be used to customize any of the
|
|
|
|
defaults.
|
|
|
|
|
|
|
|
=================== =======================================================
|
|
|
|
Keyword Arguments Description
|
|
|
|
=================== =======================================================
|
|
|
|
``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 :setting:`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 :setting:`GEOIP_COUNTRY` setting.
|
|
|
|
|
|
|
|
``city`` The name of the GeoIP city data file. Defaults to
|
|
|
|
``GeoLite2-City.mmdb``. Setting this keyword overrides
|
|
|
|
the :setting:`GEOIP_CITY` setting.
|
|
|
|
=================== =======================================================
|
|
|
|
|
2017-08-23 10:11:49 +00:00
|
|
|
Methods
|
|
|
|
=======
|
2015-07-29 21:21:03 +00:00
|
|
|
|
|
|
|
Instantiating
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. classmethod:: GeoIP2.open(path, cache)
|
|
|
|
|
|
|
|
This classmethod instantiates the GeoIP object from the given database path
|
|
|
|
and given cache setting.
|
|
|
|
|
2021-04-05 00:24:52 +00:00
|
|
|
.. deprecated:: 5.1
|
|
|
|
|
|
|
|
Use the :class:`GeoIP2()` constructor instead.
|
|
|
|
|
2015-07-29 21:21:03 +00:00
|
|
|
Querying
|
|
|
|
--------
|
|
|
|
|
2024-01-07 07:15:40 +00:00
|
|
|
All the following querying routines may take an instance of
|
|
|
|
:class:`~ipaddress.IPv4Address` or :class:`~ipaddress.IPv6Address`, a string IP
|
|
|
|
address, or a fully qualified domain name (FQDN). For example,
|
|
|
|
``IPv4Address("205.186.163.125")``, ``"205.186.163.125"``, and
|
|
|
|
``"djangoproject.com"`` would all be valid query parameters.
|
2015-07-29 21:21:03 +00:00
|
|
|
|
|
|
|
.. method:: GeoIP2.city(query)
|
|
|
|
|
|
|
|
Returns a dictionary of city information for the given query. Some
|
|
|
|
of the values in the dictionary may be undefined (``None``).
|
|
|
|
|
|
|
|
.. method:: GeoIP2.country(query)
|
|
|
|
|
|
|
|
Returns a dictionary with the country code and country for the given
|
|
|
|
query.
|
|
|
|
|
|
|
|
.. method:: GeoIP2.country_code(query)
|
|
|
|
|
|
|
|
Returns the country code corresponding to the query.
|
|
|
|
|
|
|
|
.. method:: GeoIP2.country_name(query)
|
|
|
|
|
|
|
|
Returns the country name corresponding to the query.
|
|
|
|
|
|
|
|
Coordinate Retrieval
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
.. method:: GeoIP2.coords(query)
|
|
|
|
|
|
|
|
Returns a coordinate tuple of (longitude, latitude).
|
|
|
|
|
2023-11-30 09:51:42 +00:00
|
|
|
.. deprecated:: 5.1
|
|
|
|
|
|
|
|
Use :meth:`.GeoIP2.lon_lat` instead.
|
|
|
|
|
2015-07-29 21:21:03 +00:00
|
|
|
.. method:: GeoIP2.lon_lat(query)
|
|
|
|
|
|
|
|
Returns a coordinate tuple of (longitude, latitude).
|
|
|
|
|
|
|
|
.. method:: GeoIP2.lat_lon(query)
|
|
|
|
|
|
|
|
Returns a coordinate tuple of (latitude, longitude),
|
|
|
|
|
|
|
|
.. method:: GeoIP2.geos(query)
|
|
|
|
|
|
|
|
Returns a :class:`~django.contrib.gis.geos.Point` object corresponding to the
|
|
|
|
query.
|
|
|
|
|
2017-08-23 10:25:42 +00:00
|
|
|
Settings
|
|
|
|
========
|
|
|
|
|
|
|
|
.. setting:: GEOIP_PATH
|
|
|
|
|
|
|
|
``GEOIP_PATH``
|
|
|
|
--------------
|
|
|
|
|
2019-08-10 09:55:22 +00:00
|
|
|
A string or :class:`pathlib.Path` specifying the directory where the GeoIP data
|
|
|
|
files are located. This setting is *required* unless manually specified
|
2017-08-23 10:25:42 +00:00
|
|
|
with ``path`` keyword when initializing the :class:`GeoIP2` object.
|
|
|
|
|
|
|
|
.. setting:: GEOIP_COUNTRY
|
|
|
|
|
|
|
|
``GEOIP_COUNTRY``
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
The basename to use for the GeoIP country data file. Defaults to
|
|
|
|
``'GeoLite2-Country.mmdb'``.
|
|
|
|
|
|
|
|
.. setting:: GEOIP_CITY
|
|
|
|
|
|
|
|
``GEOIP_CITY``
|
|
|
|
--------------
|
|
|
|
|
|
|
|
The basename to use for the GeoIP city data file. Defaults to
|
|
|
|
``'GeoLite2-City.mmdb'``.
|
|
|
|
|
2017-05-18 13:12:28 +00:00
|
|
|
Exceptions
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. exception:: GeoIP2Exception
|
|
|
|
|
|
|
|
The exception raised when an error occurs in a call to the underlying
|
|
|
|
``geoip2`` library.
|
|
|
|
|
2015-07-29 21:21:03 +00:00
|
|
|
.. rubric:: Footnotes
|
|
|
|
.. [#] GeoIP(R) is a registered trademark of MaxMind, Inc.
|