new: [server] basic HTTP server for the CPE guesser

This commit is contained in:
Alexandre Dulaunoy 2021-09-17 07:45:01 +02:00
parent f3d8ae9e6e
commit 56cfdd5852
Signed by: adulau
GPG key ID: 09E2CD4944E6CBCD

51
bin/server.py Normal file
View file

@ -0,0 +1,51 @@
import falcon
from wsgiref.simple_server import make_server
import requests
import redis
from datetime import datetime
import json
rdb = redis.Redis(host='127.0.0.1', port=6379, db=8, decode_responses=True)
class Search():
def on_post(self, req, resp):
ret = []
data_post = req.bounded_stream.read()
js = data_post.decode('utf-8')
q = json.loads(js)
if 'query' in q:
pass
else:
resp.status = falcon.HTTP_500
resp.media = "Missing query array or incorrect JSON format"
return
k=[]
for keyword in q['query']:
k.append('w:{}'.format(keyword.lower()))
maxinter = len(k)
cpes = []
for x in reversed(range(maxinter)):
ret = rdb.sinter(k[x])
cpes.append(list(ret))
result = set(cpes[0]).intersection(*cpes)
ranked = []
for cpe in result:
rank = rdb.zrank('rank:cpe', cpe)
ranked.append((rank, cpe))
resp.media=sorted(ranked)
app = falcon.App()
app.add_route('/search', Search())
if __name__ == '__main__':
with make_server('', 8000, app) as httpd:
print('Serving on port 8000...')
httpd.serve_forever()