Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import os
- my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
- sys.path.insert(0, my_dir + '/gen-py')
- sys.path.insert(0, my_dir + '/../../code/configs/autogen/')
- from amie_config import config as AmieConfig
- import GeoIP
- import struct
- import socket
- from geoip import GeoIPService
- from geoip.ttypes import *
- from thrift.transport import TTransport
- from thrift.transport import TSocket
- from thrift.protocol import TBinaryProtocol
- from thrift.server import TServer
- class GeoIPHandler:
- def __init__(self, path):
- self.geoip = GeoIP.open(path, GeoIP.GEOIP_MEMORY_CACHE)
- def ipTo2CharISO(self, ip_packed):
- ip_string = socket.inet_ntoa(ip_packed)
- print "ip string: ", ip_string
- result = self.geoip.country_code_by_addr(ip_string)
- print "result: ", result
- if result == None:
- return '??'
- return result
- port = AmieConfig['thrift_configs']['GeoIPService']['port']
- data = AmieConfig['geoip']['data_file']
- handler = GeoIPHandler(data)
- processor = GeoIPService.Processor(handler)
- transport = TSocket.TServerSocket(port)
- tfactory = TTransport.TBufferedTransportFactory()
- pfactory = TBinaryProtocol.TBinaryProtocolFactory()
- server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
- server.serve()
Add Comment
Please, Sign In to add comment