geoip2.txt 5.5 KB

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