geoip.txt 6.6 KB

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