geoip2.txt 5.7 KB

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