PYTHON   28

libgeoip.py

Guest on 3rd June 2021 05:58:40 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.