You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

libgeoip.py 1.0 KiB

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. from ctypes import CDLL
  3. from ctypes.util import find_library
  4. from django.conf import settings
  5. # Creating the settings dictionary with any settings, if needed.
  6. GEOIP_SETTINGS = {key: getattr(settings, key)
  7. for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY')
  8. if hasattr(settings, key)}
  9. lib_path = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH')
  10. # The shared library for the GeoIP C API. May be downloaded
  11. # from http://www.maxmind.com/download/geoip/api/c/
  12. if lib_path:
  13. lib_name = None
  14. else:
  15. # TODO: Is this really the library name for Windows?
  16. lib_name = 'GeoIP'
  17. # Getting the path to the GeoIP library.
  18. if lib_name:
  19. lib_path = find_library(lib_name)
  20. if lib_path is None:
  21. raise RuntimeError('Could not find the GeoIP library (tried "%s"). '
  22. 'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name)
  23. lgeoip = CDLL(lib_path)
  24. # Getting the C `free` for the platform.
  25. if os.name == 'nt':
  26. libc = CDLL('msvcrt')
  27. else:
  28. libc = CDLL(None)
  29. free = libc.free