geoip2.txt 6.1 KB

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