Use tornado coroutines, remove dep on iptools

This commit is contained in:
Raphaël Vinot 2016-08-29 16:14:47 +02:00
parent 48adcde238
commit 1878f64c45
2 changed files with 57 additions and 12 deletions

View file

@ -8,7 +8,6 @@ Requirements
- Python 3 - Python 3
- [Tornado](http://www.tornadoweb.org) - [Tornado](http://www.tornadoweb.org)
- Python [iptools](https://github.com/bd808/python-iptools)
- Python [redis](https://pypi.python.org/pypi/redis/) client - Python [redis](https://pypi.python.org/pypi/redis/) client
Running the qof-server Running the qof-server

View file

@ -18,7 +18,7 @@ import tornado.escape
import tornado.ioloop import tornado.ioloop
import tornado.web import tornado.web
import iptools from ipaddress import ip_address
import redis import redis
import json import json
import sys import sys
@ -115,25 +115,71 @@ class InfoHandler(tornado.web.RequestHandler):
self.write(response) self.write(response)
def is_ip(q):
try:
ip_address(q)
return True
except:
return False
class QueryHandler(tornado.web.RequestHandler): class QueryHandler(tornado.web.RequestHandler):
# Default value in Python 3.5
# https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
nb_threads = tornado.process.cpu_count() * 5
executor = ThreadPoolExecutor(nb_threads)
@run_on_executor
def run_request(self, q):
to_return = []
if is_ip(q):
for x in getAssociatedRecords(q):
to_return.append(JsonQOF(getRecord(x)))
else:
to_return.append(JsonQOF(getRecord(t=q.strip())))
return to_return
@tornado.gen.coroutine
def get(self, q): def get(self, q):
print("query: " + q) print("query: " + q)
if iptools.ipv4.validate_ip(q) or iptools.ipv6.validate_ip(q): try:
for x in getAssociatedRecords(q): responses = yield self.run_request(q)
self.write(JsonQOF(getRecord(x))) for r in responses:
else: self.write(r)
self.write(JsonQOF(getRecord(t=q.strip()))) except Exception as e:
print('Something went wrong with {}:\n{}'.format(q, e))
finally:
self.finish()
class FullQueryHandler(tornado.web.RequestHandler): class FullQueryHandler(tornado.web.RequestHandler):
def get(self, q): # Default value in Python 3.5
print("fquery: " + q) # https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
if iptools.ipv4.validate_ip(q) or iptools.ipv6.validate_ip(q): nb_threads = tornado.process.cpu_count() * 5
executor = ThreadPoolExecutor(nb_threads)
@run_on_executor
def run_request(self, q):
to_return = []
if is_ip(q):
for x in getAssociatedRecords(q): for x in getAssociatedRecords(q):
self.write(JsonQOF(getRecord(x))) to_return.append(JsonQOF(getRecord(x)))
else: else:
for x in getAssociatedRecords(q): for x in getAssociatedRecords(q):
self.write(JsonQOF(getRecord(t=x.strip()))) to_return.append(JsonQOF(getRecord(t=x.strip())))
return to_return
def get(self, q):
print("fquery: " + q)
try:
responses = yield self.run_request(q)
for r in responses:
self.write(r)
except Exception as e:
print('Something went wrong with {}:\n{}'.format(q, e))
finally:
self.finish()
def main(): def main():