geoip2.txt 5.7 KB

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