geoip.txt 7.1 KB

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