def process(self, job): line = job.data if statsd is not None: timer = statsd.Timer('GeoIP') req_counter = statsd.counter('GeoIP.request') success_counter = statsd.counter('GeoIP.success') fail_counter = statsd.counter('GeoIP.failure') timer.start() else: timer = None req_counter = 0 success_counter = 0 fail_counter = 0 try: if 'monitor' == line.strip(): return self.checkConnection() if ' ' not in line: return self._error('Invalid request. Try "GET addr"') items = line.split(' ') cmd = items[0] addr = items[1].strip().replace('/', '') if cmd.upper() != 'GET': return self._error('Invalid command. Try "GET addr"') if addr is None: return self._error('Missing address') try: req_counter += 1 reply = self.geoip.city(addr) if reply is None: fail_counter += 1 return self._error('No information for site') success_counter += 1 reply.update({'addr': addr}) return self._return('success', reply) except Exception as e: fail_counter += 1 return self._error('Unknown Exception "%s"' % str(e)) finally: if timer: timer.stop('Request')
def handle(self, socket, address): if statsd is not None: timer = statsd.Timer('GeoIP') req_counter = statsd.counter('GeoIP.request') success_counter = statsd.counter('GeoIP.success') fail_counter = statsd.counter('GeoIP.failure') timer.start() else: timer = None req_counter = 0 success_counter = 0 fail_counter = 0 try: sock = socket.makefile() line = sock.readline() req_counter += 1 if ' ' not in line: return sock.write(self._error('Invalid request\nGET addr')) items = line.split(' ') cmd = items[0] addr = items[1].strip().replace('/', '') if cmd.upper() != 'GET': return sock.write(self._error('Invalid Command\nuse GET ')) if addr is None: return sock.write(self._error('Missing address')) if 'monitor' in addr: sock.write(self.checkConnection(sock)) return sock.flush() reply = self.geoip.city(addr) if reply is None: fail_counter += 1 return sock.write(self._error('No information for site')) else: success_counter += 1 return sock.write(self._return('success', reply)) except Exception, e: return sock.write(self._error("Unknown error: %s %s" % (e, line)))