geoip.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ======================
  2. Geolocation with GeoIP
  3. ======================
  4. .. module:: django.contrib.gis.geoip
  5. :synopsis: High-level Python interface for MaxMind's GeoIP C library.
  6. .. deprecated:: 1.9
  7. This module is deprecated in favor of :doc:`django.contrib.gis.geoip2
  8. </ref/contrib/gis/geoip2>`, which supports IPv6 and the GeoLite2 database
  9. format.
  10. The :class:`GeoIP` object is a ctypes wrapper for the
  11. `MaxMind GeoIP C API`__. [#]_
  12. In order to perform IP-based geolocation, the :class:`GeoIP` object requires
  13. the GeoIP C library and either the GeoIP `Country`__ or `City`__
  14. datasets in binary format (the CSV files will not work!). These datasets may be
  15. `downloaded from MaxMind`__. Grab the ``GeoLiteCountry/GeoIP.dat.gz`` and
  16. ``GeoLiteCity.dat.gz`` files and unzip them in a directory corresponding to what
  17. you set :setting:`GEOIP_PATH` with in your settings. See the example and
  18. reference below for more details.
  19. __ https://www.maxmind.com/app/c
  20. __ https://www.maxmind.com/app/country
  21. __ https://www.maxmind.com/app/city
  22. __ https://www.maxmind.com/download/geoip/database/
  23. Example
  24. =======
  25. Assuming you have the GeoIP C library installed, here is an example of its
  26. usage::
  27. >>> from django.contrib.gis.geoip import GeoIP
  28. >>> g = GeoIP()
  29. >>> g.country('google.com')
  30. {'country_code': 'US', 'country_name': 'United States'}
  31. >>> g.city('72.14.207.99')
  32. {'area_code': 650,
  33. 'city': 'Mountain View',
  34. 'country_code': 'US',
  35. 'country_code3': 'USA',
  36. 'country_name': 'United States',
  37. 'dma_code': 807,
  38. 'latitude': 37.419200897216797,
  39. 'longitude': -122.05740356445312,
  40. 'postal_code': '94043',
  41. 'region': 'CA'}
  42. >>> g.lat_lon('salon.com')
  43. (37.789798736572266, -122.39420318603516)
  44. >>> g.lon_lat('uh.edu')
  45. (-95.415199279785156, 29.77549934387207)
  46. >>> g.geos('24.124.1.80').wkt
  47. 'POINT (-95.2087020874023438 39.0392990112304688)'
  48. ``GeoIP`` Settings
  49. ==================
  50. .. setting:: GEOIP_PATH
  51. ``GEOIP_PATH``
  52. --------------
  53. A string specifying the directory where the GeoIP data files are
  54. located. This setting is *required* unless manually specified
  55. with ``path`` keyword when initializing the :class:`GeoIP` object.
  56. .. setting:: GEOIP_LIBRARY_PATH
  57. ``GEOIP_LIBRARY_PATH``
  58. ----------------------
  59. A string specifying the location of the GeoIP C library. Typically,
  60. this setting is only used if the GeoIP C library is in a non-standard
  61. location (e.g., ``/home/sue/lib/libGeoIP.so``).
  62. .. setting:: GEOIP_COUNTRY
  63. ``GEOIP_COUNTRY``
  64. -----------------
  65. The basename to use for the GeoIP country data file.
  66. Defaults to ``'GeoIP.dat'``.
  67. .. setting:: GEOIP_CITY
  68. ``GEOIP_CITY``
  69. --------------
  70. The basename to use for the GeoIP city data file.
  71. Defaults to ``'GeoLiteCity.dat'``.
  72. ``GeoIP`` API
  73. =============
  74. .. class:: GeoIP(path=None, cache=0, country=None, city=None)
  75. The ``GeoIP`` object does not require any parameters to use the default
  76. settings. However, at the very least the :setting:`GEOIP_PATH` setting
  77. should be set with the path of the location of your GeoIP data sets. The
  78. following initialization keywords may be used to customize any of the
  79. defaults.
  80. =================== =======================================================
  81. Keyword Arguments Description
  82. =================== =======================================================
  83. ``path`` Base directory to where GeoIP data is located or the
  84. full path to where the city or country data files
  85. (.dat) are located. Assumes that both the city and
  86. country data sets are located in this directory;
  87. overrides the :setting:`GEOIP_PATH` settings attribute.
  88. ``cache`` The cache settings when opening up the GeoIP datasets,
  89. and may be an integer in (0, 1, 2, 4) corresponding to
  90. the ``GEOIP_STANDARD``, ``GEOIP_MEMORY_CACHE``,
  91. ``GEOIP_CHECK_CACHE``, and ``GEOIP_INDEX_CACHE``
  92. ``GeoIPOptions`` C API settings, respectively.
  93. Defaults to 0 (``GEOIP_STANDARD``).
  94. ``country`` The name of the GeoIP country data file. Defaults
  95. to ``GeoIP.dat``. Setting this keyword overrides the
  96. :setting:`GEOIP_COUNTRY` settings attribute.
  97. ``city`` The name of the GeoIP city data file. Defaults to
  98. ``GeoLiteCity.dat``. Setting this keyword overrides
  99. the :setting:`GEOIP_CITY` settings attribute.
  100. =================== =======================================================
  101. ``GeoIP`` Methods
  102. =================
  103. Querying
  104. --------
  105. All the following querying routines may take either a string IP address
  106. or a fully qualified domain name (FQDN). For example, both
  107. ``'205.186.163.125'`` and ``'djangoproject.com'`` would be valid query
  108. parameters.
  109. .. method:: GeoIP.city(query)
  110. Returns a dictionary of city information for the given query. Some
  111. of the values in the dictionary may be undefined (``None``).
  112. .. method:: GeoIP.country(query)
  113. Returns a dictionary with the country code and country for the given
  114. query.
  115. .. method:: GeoIP.country_code(query)
  116. Returns only the country code corresponding to the query.
  117. .. method:: GeoIP.country_name(query)
  118. Returns only the country name corresponding to the query.
  119. Coordinate Retrieval
  120. --------------------
  121. .. method:: GeoIP.coords(query)
  122. Returns a coordinate tuple of (longitude, latitude).
  123. .. method:: GeoIP.lon_lat(query)
  124. Returns a coordinate tuple of (longitude, latitude).
  125. .. method:: GeoIP.lat_lon(query)
  126. Returns a coordinate tuple of (latitude, longitude),
  127. .. method:: GeoIP.geos(query)
  128. Returns a :class:`django.contrib.gis.geos.Point` object corresponding to the query.
  129. Database Information
  130. --------------------
  131. .. attribute:: GeoIP.country_info
  132. This property returns information about the GeoIP country database.
  133. .. attribute:: GeoIP.city_info
  134. This property returns information about the GeoIP city database.
  135. .. attribute:: GeoIP.info
  136. This property returns information about all GeoIP databases (both city
  137. and country), and the version of the GeoIP C library (if supported).
  138. GeoIP-Python API compatibility methods
  139. ----------------------------------------
  140. These methods exist to ease compatibility with any code using MaxMind's
  141. existing Python API.
  142. .. classmethod:: GeoIP.open(path, cache)
  143. This classmethod instantiates the GeoIP object from the given database path
  144. and given cache setting.
  145. .. method:: GeoIP.region_by_addr(query)
  146. .. method:: GeoIP.region_by_name(query)
  147. .. method:: GeoIP.record_by_addr(query)
  148. .. method:: GeoIP.record_by_name(query)
  149. .. method:: GeoIP.country_code_by_addr(query)
  150. .. method:: GeoIP.country_code_by_name(query)
  151. .. method:: GeoIP.country_name_by_addr(query)
  152. .. method:: GeoIP.country_name_by_name(query)
  153. .. rubric:: Footnotes
  154. .. [#] GeoIP(R) is a registered trademark of MaxMind, LLC of Boston, Massachusetts.