From 1878f64c45903b287757c168fc2fd16976bb1a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Mon, 29 Aug 2016 16:14:47 +0200 Subject: [PATCH] Use tornado coroutines, remove dep on iptools --- README.md | 1 - bin/qof-server.py | 68 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 756d995..bc15fec 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ Requirements - Python 3 - [Tornado](http://www.tornadoweb.org) -- Python [iptools](https://github.com/bd808/python-iptools) - Python [redis](https://pypi.python.org/pypi/redis/) client Running the qof-server diff --git a/bin/qof-server.py b/bin/qof-server.py index e3fdf43..f707754 100644 --- a/bin/qof-server.py +++ b/bin/qof-server.py @@ -18,7 +18,7 @@ import tornado.escape import tornado.ioloop import tornado.web -import iptools +from ipaddress import ip_address import redis import json import sys @@ -115,25 +115,71 @@ class InfoHandler(tornado.web.RequestHandler): self.write(response) +def is_ip(q): + try: + ip_address(q) + return True + except: + return False + + 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): print("query: " + q) - if iptools.ipv4.validate_ip(q) or iptools.ipv6.validate_ip(q): - for x in getAssociatedRecords(q): - self.write(JsonQOF(getRecord(x))) - else: - self.write(JsonQOF(getRecord(t=q.strip()))) + 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() class FullQueryHandler(tornado.web.RequestHandler): - def get(self, q): - print("fquery: " + q) - if iptools.ipv4.validate_ip(q) or iptools.ipv6.validate_ip(q): + # 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): - self.write(JsonQOF(getRecord(x))) + to_return.append(JsonQOF(getRecord(x))) else: 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():