Guest User

Untitled

a guest
May 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import os
  5.  
  6. my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  7.  
  8. sys.path.insert(0, my_dir + '/gen-py')
  9. sys.path.insert(0, my_dir + '/../../code/configs/autogen/')
  10.  
  11. from amie_config import config as AmieConfig
  12.  
  13. import GeoIP
  14. import struct
  15. import socket
  16.  
  17. from geoip import GeoIPService
  18. from geoip.ttypes import *
  19. from thrift.transport import TTransport
  20. from thrift.transport import TSocket
  21. from thrift.protocol import TBinaryProtocol
  22. from thrift.server import TServer
  23.  
  24. class GeoIPHandler:
  25. def __init__(self, path):
  26. self.geoip = GeoIP.open(path, GeoIP.GEOIP_MEMORY_CACHE)
  27.  
  28. def ipTo2CharISO(self, ip_packed):
  29. ip_string = socket.inet_ntoa(ip_packed)
  30. print "ip string: ", ip_string
  31. result = self.geoip.country_code_by_addr(ip_string)
  32. print "result: ", result
  33. if result == None:
  34. return '??'
  35. return result
  36.  
  37.  
  38.  
  39. port = AmieConfig['thrift_configs']['GeoIPService']['port']
  40. data = AmieConfig['geoip']['data_file']
  41.  
  42. handler = GeoIPHandler(data)
  43. processor = GeoIPService.Processor(handler)
  44. transport = TSocket.TServerSocket(port)
  45. tfactory = TTransport.TBufferedTransportFactory()
  46. pfactory = TBinaryProtocol.TBinaryProtocolFactory()
  47. server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
  48. server.serve()
Add Comment
Please, Sign In to add comment