/django/contrib/gis/utils/geoip.py

https://code.google.com/p/mango-py/ · Python · 361 lines · 341 code · 0 blank · 20 comment · 3 complexity · 187ea5801ac7d86bbf16b350ae0c7d49 MD5 · raw file

  1. """
  2. This module houses the GeoIP object, a ctypes wrapper for the MaxMind GeoIP(R)
  3. C API (http://www.maxmind.com/app/c). This is an alternative to the GPL
  4. licensed Python GeoIP interface provided by MaxMind.
  5. GeoIP(R) is a registered trademark of MaxMind, LLC of Boston, Massachusetts.
  6. For IP-based geolocation, this module requires the GeoLite Country and City
  7. datasets, in binary format (CSV will not work!). The datasets may be
  8. downloaded from MaxMind at http://www.maxmind.com/download/geoip/database/.
  9. Grab GeoIP.dat.gz and GeoLiteCity.dat.gz, and unzip them in the directory
  10. corresponding to settings.GEOIP_PATH. See the GeoIP docstring and examples
  11. below for more details.
  12. TODO: Verify compatibility with Windows.
  13. Example:
  14. >>> from django.contrib.gis.utils import GeoIP
  15. >>> g = GeoIP()
  16. >>> g.country('google.com')
  17. {'country_code': 'US', 'country_name': 'United States'}
  18. >>> g.city('72.14.207.99')
  19. {'area_code': 650,
  20. 'city': 'Mountain View',
  21. 'country_code': 'US',
  22. 'country_code3': 'USA',
  23. 'country_name': 'United States',
  24. 'dma_code': 807,
  25. 'latitude': 37.419200897216797,
  26. 'longitude': -122.05740356445312,
  27. 'postal_code': '94043',
  28. 'region': 'CA'}
  29. >>> g.lat_lon('salon.com')
  30. (37.789798736572266, -122.39420318603516)
  31. >>> g.lon_lat('uh.edu')
  32. (-95.415199279785156, 29.77549934387207)
  33. >>> g.geos('24.124.1.80').wkt
  34. 'POINT (-95.2087020874023438 39.0392990112304688)'
  35. """
  36. import os, re
  37. from ctypes import c_char_p, c_float, c_int, Structure, CDLL, POINTER
  38. from ctypes.util import find_library
  39. from django.conf import settings
  40. if not settings.configured: settings.configure()
  41. # Creating the settings dictionary with any settings, if needed.
  42. GEOIP_SETTINGS = dict((key, getattr(settings, key))
  43. for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY')
  44. if hasattr(settings, key))
  45. lib_path = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH', None)
  46. # GeoIP Exception class.
  47. class GeoIPException(Exception): pass
  48. # The shared library for the GeoIP C API. May be downloaded
  49. # from http://www.maxmind.com/download/geoip/api/c/
  50. if lib_path:
  51. lib_name = None
  52. else:
  53. # TODO: Is this really the library name for Windows?
  54. lib_name = 'GeoIP'
  55. # Getting the path to the GeoIP library.
  56. if lib_name: lib_path = find_library(lib_name)
  57. if lib_path is None: raise GeoIPException('Could not find the GeoIP library (tried "%s"). '
  58. 'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name)
  59. lgeoip = CDLL(lib_path)
  60. # Regular expressions for recognizing IP addresses and the GeoIP
  61. # free database editions.
  62. ipregex = re.compile(r'^(?P<w>\d\d?\d?)\.(?P<x>\d\d?\d?)\.(?P<y>\d\d?\d?)\.(?P<z>\d\d?\d?)$')
  63. free_regex = re.compile(r'^GEO-\d{3}FREE')
  64. lite_regex = re.compile(r'^GEO-\d{3}LITE')
  65. #### GeoIP C Structure definitions ####
  66. class GeoIPRecord(Structure):
  67. _fields_ = [('country_code', c_char_p),
  68. ('country_code3', c_char_p),
  69. ('country_name', c_char_p),
  70. ('region', c_char_p),
  71. ('city', c_char_p),
  72. ('postal_code', c_char_p),
  73. ('latitude', c_float),
  74. ('longitude', c_float),
  75. # TODO: In 1.4.6 this changed from `int dma_code;` to
  76. # `union {int metro_code; int dma_code;};`. Change
  77. # to a `ctypes.Union` in to accomodate in future when
  78. # pre-1.4.6 versions are no longer distributed.
  79. ('dma_code', c_int),
  80. ('area_code', c_int),
  81. # TODO: The following structure fields were added in 1.4.3 --
  82. # uncomment these fields when sure previous versions are no
  83. # longer distributed by package maintainers.
  84. #('charset', c_int),
  85. #('continent_code', c_char_p),
  86. ]
  87. class GeoIPTag(Structure): pass
  88. #### ctypes function prototypes ####
  89. RECTYPE = POINTER(GeoIPRecord)
  90. DBTYPE = POINTER(GeoIPTag)
  91. # For retrieving records by name or address.
  92. def record_output(func):
  93. func.restype = RECTYPE
  94. return func
  95. rec_by_addr = record_output(lgeoip.GeoIP_record_by_addr)
  96. rec_by_name = record_output(lgeoip.GeoIP_record_by_name)
  97. # For opening & closing GeoIP database files.
  98. geoip_open = lgeoip.GeoIP_open
  99. geoip_open.restype = DBTYPE
  100. geoip_close = lgeoip.GeoIP_delete
  101. geoip_close.argtypes = [DBTYPE]
  102. geoip_close.restype = None
  103. # String output routines.
  104. def string_output(func):
  105. func.restype = c_char_p
  106. return func
  107. geoip_dbinfo = string_output(lgeoip.GeoIP_database_info)
  108. cntry_code_by_addr = string_output(lgeoip.GeoIP_country_code_by_addr)
  109. cntry_code_by_name = string_output(lgeoip.GeoIP_country_code_by_name)
  110. cntry_name_by_addr = string_output(lgeoip.GeoIP_country_name_by_addr)
  111. cntry_name_by_name = string_output(lgeoip.GeoIP_country_name_by_name)
  112. #### GeoIP class ####
  113. class GeoIP(object):
  114. # The flags for GeoIP memory caching.
  115. # GEOIP_STANDARD - read database from filesystem, uses least memory.
  116. #
  117. # GEOIP_MEMORY_CACHE - load database into memory, faster performance
  118. # but uses more memory
  119. #
  120. # GEOIP_CHECK_CACHE - check for updated database. If database has been updated,
  121. # reload filehandle and/or memory cache.
  122. #
  123. # GEOIP_INDEX_CACHE - just cache
  124. # the most frequently accessed index portion of the database, resulting
  125. # in faster lookups than GEOIP_STANDARD, but less memory usage than
  126. # GEOIP_MEMORY_CACHE - useful for larger databases such as
  127. # GeoIP Organization and GeoIP City. Note, for GeoIP Country, Region
  128. # and Netspeed databases, GEOIP_INDEX_CACHE is equivalent to GEOIP_MEMORY_CACHE
  129. #
  130. GEOIP_STANDARD = 0
  131. GEOIP_MEMORY_CACHE = 1
  132. GEOIP_CHECK_CACHE = 2
  133. GEOIP_INDEX_CACHE = 4
  134. cache_options = dict((opt, None) for opt in (0, 1, 2, 4))
  135. _city_file = ''
  136. _country_file = ''
  137. # Initially, pointers to GeoIP file references are NULL.
  138. _city = None
  139. _country = None
  140. def __init__(self, path=None, cache=0, country=None, city=None):
  141. """
  142. Initializes the GeoIP object, no parameters are required to use default
  143. settings. Keyword arguments may be passed in to customize the locations
  144. of the GeoIP data sets.
  145. * path: Base directory to where GeoIP data is located or the full path
  146. to where the city or country data files (*.dat) are located.
  147. Assumes that both the city and country data sets are located in
  148. this directory; overrides the GEOIP_PATH settings attribute.
  149. * cache: The cache settings when opening up the GeoIP datasets,
  150. and may be an integer in (0, 1, 2, 4) corresponding to
  151. the GEOIP_STANDARD, GEOIP_MEMORY_CACHE, GEOIP_CHECK_CACHE,
  152. and GEOIP_INDEX_CACHE `GeoIPOptions` C API settings,
  153. respectively. Defaults to 0, meaning that the data is read
  154. from the disk.
  155. * country: The name of the GeoIP country data file. Defaults to
  156. 'GeoIP.dat'; overrides the GEOIP_COUNTRY settings attribute.
  157. * city: The name of the GeoIP city data file. Defaults to
  158. 'GeoLiteCity.dat'; overrides the GEOIP_CITY settings attribute.
  159. """
  160. # Checking the given cache option.
  161. if cache in self.cache_options:
  162. self._cache = self.cache_options[cache]
  163. else:
  164. raise GeoIPException('Invalid caching option: %s' % cache)
  165. # Getting the GeoIP data path.
  166. if not path:
  167. path = GEOIP_SETTINGS.get('GEOIP_PATH', None)
  168. if not path: raise GeoIPException('GeoIP path must be provided via parameter or the GEOIP_PATH setting.')
  169. if not isinstance(path, basestring):
  170. raise TypeError('Invalid path type: %s' % type(path).__name__)
  171. if os.path.isdir(path):
  172. # Constructing the GeoIP database filenames using the settings
  173. # dictionary. If the database files for the GeoLite country
  174. # and/or city datasets exist, then try and open them.
  175. country_db = os.path.join(path, country or GEOIP_SETTINGS.get('GEOIP_COUNTRY', 'GeoIP.dat'))
  176. if os.path.isfile(country_db):
  177. self._country = geoip_open(country_db, cache)
  178. self._country_file = country_db
  179. city_db = os.path.join(path, city or GEOIP_SETTINGS.get('GEOIP_CITY', 'GeoLiteCity.dat'))
  180. if os.path.isfile(city_db):
  181. self._city = geoip_open(city_db, cache)
  182. self._city_file = city_db
  183. elif os.path.isfile(path):
  184. # Otherwise, some detective work will be needed to figure
  185. # out whether the given database path is for the GeoIP country
  186. # or city databases.
  187. ptr = geoip_open(path, cache)
  188. info = geoip_dbinfo(ptr)
  189. if lite_regex.match(info):
  190. # GeoLite City database detected.
  191. self._city = ptr
  192. self._city_file = path
  193. elif free_regex.match(info):
  194. # GeoIP Country database detected.
  195. self._country = ptr
  196. self._country_file = path
  197. else:
  198. raise GeoIPException('Unable to recognize database edition: %s' % info)
  199. else:
  200. raise GeoIPException('GeoIP path must be a valid file or directory.')
  201. def __del__(self):
  202. # Cleaning any GeoIP file handles lying around.
  203. if self._country: geoip_close(self._country)
  204. if self._city: geoip_close(self._city)
  205. def _check_query(self, query, country=False, city=False, city_or_country=False):
  206. "Helper routine for checking the query and database availability."
  207. # Making sure a string was passed in for the query.
  208. if not isinstance(query, basestring):
  209. raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
  210. # Extra checks for the existence of country and city databases.
  211. if city_or_country and not (self._country or self._city):
  212. raise GeoIPException('Invalid GeoIP country and city data files.')
  213. elif country and not self._country:
  214. raise GeoIPException('Invalid GeoIP country data file: %s' % self._country_file)
  215. elif city and not self._city:
  216. raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file)
  217. def city(self, query):
  218. """
  219. Returns a dictionary of city information for the given IP address or
  220. Fully Qualified Domain Name (FQDN). Some information in the dictionary
  221. may be undefined (None).
  222. """
  223. self._check_query(query, city=True)
  224. if ipregex.match(query):
  225. # If an IP address was passed in
  226. ptr = rec_by_addr(self._city, c_char_p(query))
  227. else:
  228. # If a FQDN was passed in.
  229. ptr = rec_by_name(self._city, c_char_p(query))
  230. # Checking the pointer to the C structure, if valid pull out elements
  231. # into a dicionary and return.
  232. if bool(ptr):
  233. record = ptr.contents
  234. return dict((tup[0], getattr(record, tup[0])) for tup in record._fields_)
  235. else:
  236. return None
  237. def country_code(self, query):
  238. "Returns the country code for the given IP Address or FQDN."
  239. self._check_query(query, city_or_country=True)
  240. if self._country:
  241. if ipregex.match(query): return cntry_code_by_addr(self._country, query)
  242. else: return cntry_code_by_name(self._country, query)
  243. else:
  244. return self.city(query)['country_code']
  245. def country_name(self, query):
  246. "Returns the country name for the given IP Address or FQDN."
  247. self._check_query(query, city_or_country=True)
  248. if self._country:
  249. if ipregex.match(query): return cntry_name_by_addr(self._country, query)
  250. else: return cntry_name_by_name(self._country, query)
  251. else:
  252. return self.city(query)['country_name']
  253. def country(self, query):
  254. """
  255. Returns a dictonary with with the country code and name when given an
  256. IP address or a Fully Qualified Domain Name (FQDN). For example, both
  257. '24.124.1.80' and 'djangoproject.com' are valid parameters.
  258. """
  259. # Returning the country code and name
  260. return {'country_code' : self.country_code(query),
  261. 'country_name' : self.country_name(query),
  262. }
  263. #### Coordinate retrieval routines ####
  264. def coords(self, query, ordering=('longitude', 'latitude')):
  265. cdict = self.city(query)
  266. if cdict is None: return None
  267. else: return tuple(cdict[o] for o in ordering)
  268. def lon_lat(self, query):
  269. "Returns a tuple of the (longitude, latitude) for the given query."
  270. return self.coords(query)
  271. def lat_lon(self, query):
  272. "Returns a tuple of the (latitude, longitude) for the given query."
  273. return self.coords(query, ('latitude', 'longitude'))
  274. def geos(self, query):
  275. "Returns a GEOS Point object for the given query."
  276. ll = self.lon_lat(query)
  277. if ll:
  278. from django.contrib.gis.geos import Point
  279. return Point(ll, srid=4326)
  280. else:
  281. return None
  282. #### GeoIP Database Information Routines ####
  283. def country_info(self):
  284. "Returns information about the GeoIP country database."
  285. if self._country is None:
  286. ci = 'No GeoIP Country data in "%s"' % self._country_file
  287. else:
  288. ci = geoip_dbinfo(self._country)
  289. return ci
  290. country_info = property(country_info)
  291. def city_info(self):
  292. "Retuns information about the GeoIP city database."
  293. if self._city is None:
  294. ci = 'No GeoIP City data in "%s"' % self._city_file
  295. else:
  296. ci = geoip_dbinfo(self._city)
  297. return ci
  298. city_info = property(city_info)
  299. def info(self):
  300. "Returns information about all GeoIP databases in use."
  301. return 'Country:\n\t%s\nCity:\n\t%s' % (self.country_info, self.city_info)
  302. info = property(info)
  303. #### Methods for compatibility w/the GeoIP-Python API. ####
  304. @classmethod
  305. def open(cls, full_path, cache):
  306. return GeoIP(full_path, cache)
  307. def _rec_by_arg(self, arg):
  308. if self._city:
  309. return self.city(arg)
  310. else:
  311. return self.country(arg)
  312. region_by_addr = city
  313. region_by_name = city
  314. record_by_addr = _rec_by_arg
  315. record_by_name = _rec_by_arg
  316. country_code_by_addr = country_code
  317. country_code_by_name = country_code
  318. country_name_by_addr = country_name
  319. country_name_by_name = country_name