1. =======================
    
  2. Geolocation with GeoIP2
    
  3. =======================
    
  4. 
    
  5. .. module:: django.contrib.gis.geoip2
    
  6.     :synopsis: Python interface for MaxMind's GeoIP2 databases.
    
  7. 
    
  8. The :class:`GeoIP2` object is a wrapper for the `MaxMind geoip2 Python
    
  9. library`__. [#]_
    
  10. 
    
  11. In order to perform IP-based geolocation, the :class:`GeoIP2` object requires
    
  12. the `geoip2 Python library`__ and the GeoIP ``Country`` and/or ``City``
    
  13. `datasets in binary format`__ (the CSV files will not work!). Grab the
    
  14. ``GeoLite2-Country.mmdb.gz`` and ``GeoLite2-City.mmdb.gz`` files and unzip them
    
  15. in a directory corresponding to the :setting:`GEOIP_PATH` setting.
    
  16. 
    
  17. Additionally, it is recommended to install the `libmaxminddb C library`__, so
    
  18. that ``geoip2`` can leverage the C library's faster speed.
    
  19. 
    
  20. __ https://geoip2.readthedocs.io/
    
  21. __ https://pypi.org/project/geoip2/
    
  22. __ https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
    
  23. __ https://github.com/maxmind/libmaxminddb/
    
  24. 
    
  25. Example
    
  26. =======
    
  27. 
    
  28. Here is an example of its usage::
    
  29. 
    
  30.     >>> from django.contrib.gis.geoip2 import GeoIP2
    
  31.     >>> g = GeoIP2()
    
  32.     >>> g.country('google.com')
    
  33.     {'country_code': 'US', 'country_name': 'United States'}
    
  34.     >>> g.city('72.14.207.99')
    
  35.     {'city': 'Mountain View',
    
  36.     'continent_code': 'NA',
    
  37.     'continent_name': 'North America',
    
  38.     'country_code': 'US',
    
  39.     'country_name': 'United States',
    
  40.     'dma_code': 807,
    
  41.     'is_in_european_union': False,
    
  42.     'latitude': 37.419200897216797,
    
  43.     'longitude': -122.05740356445312,
    
  44.     'postal_code': '94043',
    
  45.     'region': 'CA',
    
  46.     'time_zone': 'America/Los_Angeles'}
    
  47.     >>> g.lat_lon('salon.com')
    
  48.     (39.0437, -77.4875)
    
  49.     >>> g.lon_lat('uh.edu')
    
  50.     (-95.4342, 29.834)
    
  51.     >>> g.geos('24.124.1.80').wkt
    
  52.     'POINT (-97 38)'
    
  53. 
    
  54. API Reference
    
  55. =============
    
  56. 
    
  57. .. class:: GeoIP2(path=None, cache=0, country=None, city=None)
    
  58. 
    
  59. The ``GeoIP`` object does not require any parameters to use the default
    
  60. settings. However, at the very least the :setting:`GEOIP_PATH` setting
    
  61. should be set with the path of the location of your GeoIP datasets. The
    
  62. following initialization keywords may be used to customize any of the
    
  63. defaults.
    
  64. 
    
  65. ===================  =======================================================
    
  66. Keyword Arguments    Description
    
  67. ===================  =======================================================
    
  68. ``path``             Base directory to where GeoIP data is located or the
    
  69.                      full path to where the city or country data files
    
  70.                      (``.mmdb``) are located. Assumes that both the city and
    
  71.                      country datasets are located in this directory;
    
  72.                      overrides the :setting:`GEOIP_PATH` setting.
    
  73. 
    
  74. ``cache``            The cache settings when opening up the GeoIP datasets. May
    
  75.                      be an integer in (0, 1, 2, 4, 8) corresponding to the
    
  76.                      ``MODE_AUTO``, ``MODE_MMAP_EXT``, ``MODE_MMAP``, and
    
  77.                      ``GEOIP_INDEX_CACHE`` ``MODE_MEMORY`` C API settings,
    
  78.                      respectively. Defaults to 0 (``MODE_AUTO``).
    
  79. 
    
  80. ``country``          The name of the GeoIP country data file. Defaults
    
  81.                      to ``GeoLite2-Country.mmdb``. Setting this keyword
    
  82.                      overrides the :setting:`GEOIP_COUNTRY` setting.
    
  83. 
    
  84. ``city``             The name of the GeoIP city data file. Defaults to
    
  85.                      ``GeoLite2-City.mmdb``. Setting this keyword overrides
    
  86.                      the :setting:`GEOIP_CITY` setting.
    
  87. ===================  =======================================================
    
  88. 
    
  89. Methods
    
  90. =======
    
  91. 
    
  92. Instantiating
    
  93. -------------
    
  94. 
    
  95. .. classmethod:: GeoIP2.open(path, cache)
    
  96. 
    
  97. This classmethod instantiates the GeoIP object from the given database path
    
  98. and given cache setting.
    
  99. 
    
  100. Querying
    
  101. --------
    
  102. 
    
  103. All the following querying routines may take either a string IP address
    
  104. or a fully qualified domain name (FQDN). For example, both
    
  105. ``'205.186.163.125'`` and ``'djangoproject.com'`` would be valid query
    
  106. parameters.
    
  107. 
    
  108. .. method:: GeoIP2.city(query)
    
  109. 
    
  110. Returns a dictionary of city information for the given query. Some
    
  111. of the values in the dictionary may be undefined (``None``).
    
  112. 
    
  113. .. method:: GeoIP2.country(query)
    
  114. 
    
  115. Returns a dictionary with the country code and country for the given
    
  116. query.
    
  117. 
    
  118. .. method:: GeoIP2.country_code(query)
    
  119. 
    
  120. Returns the country code corresponding to the query.
    
  121. 
    
  122. .. method:: GeoIP2.country_name(query)
    
  123. 
    
  124. Returns the country name corresponding to the query.
    
  125. 
    
  126. Coordinate Retrieval
    
  127. --------------------
    
  128. 
    
  129. .. method:: GeoIP2.coords(query)
    
  130. 
    
  131. Returns a coordinate tuple of (longitude, latitude).
    
  132. 
    
  133. .. method:: GeoIP2.lon_lat(query)
    
  134. 
    
  135. Returns a coordinate tuple of (longitude, latitude).
    
  136. 
    
  137. .. method:: GeoIP2.lat_lon(query)
    
  138. 
    
  139. Returns a coordinate tuple of (latitude, longitude),
    
  140. 
    
  141. .. method:: GeoIP2.geos(query)
    
  142. 
    
  143. Returns a :class:`~django.contrib.gis.geos.Point` object corresponding to the
    
  144. query.
    
  145. 
    
  146. Settings
    
  147. ========
    
  148. 
    
  149. .. setting:: GEOIP_PATH
    
  150. 
    
  151. ``GEOIP_PATH``
    
  152. --------------
    
  153. 
    
  154. A string or :class:`pathlib.Path` specifying the directory where the GeoIP data
    
  155. files are located. This setting is *required* unless manually specified
    
  156. with ``path`` keyword when initializing the :class:`GeoIP2` object.
    
  157. 
    
  158. .. setting:: GEOIP_COUNTRY
    
  159. 
    
  160. ``GEOIP_COUNTRY``
    
  161. -----------------
    
  162. 
    
  163. The basename to use for the GeoIP country data file. Defaults to
    
  164. ``'GeoLite2-Country.mmdb'``.
    
  165. 
    
  166. .. setting:: GEOIP_CITY
    
  167. 
    
  168. ``GEOIP_CITY``
    
  169. --------------
    
  170. 
    
  171. The basename to use for the GeoIP city data file. Defaults to
    
  172. ``'GeoLite2-City.mmdb'``.
    
  173. 
    
  174. Exceptions
    
  175. ==========
    
  176. 
    
  177. .. exception:: GeoIP2Exception
    
  178. 
    
  179.     The exception raised when an error occurs in a call to the underlying
    
  180.     ``geoip2`` library.
    
  181. 
    
  182. .. rubric:: Footnotes
    
  183. .. [#] GeoIP(R) is a registered trademark of MaxMind, Inc.