forked from momosecurity/aswan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisk_server.py
56 lines (41 loc) · 1.56 KB
/
risk_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding=utf8
"""A http server which offers two uri: query and report"""
from gevent import monkey
monkey.patch_all() # noqa
import json
from cgi import FieldStorage
from gevent.pywsgi import WSGIServer
from server import query_handler, report_handler
from config import RISK_SERVER_HOST, RISK_SERVER_PORT
URL_2_HANDLERS = {
"/query/": query_handler,
"/report/": report_handler,
}
def __parse_post_body(environ, ignore_get=False):
post_data = {}
storage = FieldStorage(fp=environ['wsgi.input'], environ=environ,
keep_blank_values=True)
# accept post json
if environ["REQUEST_METHOD"] == "POST" and environ[
"CONTENT_TYPE"] == "application/json":
post_data = json.loads(storage.value)
return post_data
# accept get querystring
if not ignore_get:
for k in storage.keys():
post_data[k] = storage.getvalue(k)
return post_data
def application(environ, start_response):
if environ['PATH_INFO'] not in URL_2_HANDLERS:
response = json.dumps({"ec": 0, "error": "invalid uri"})
start_response('200 OK', [('Content-Type', 'application/json')])
return [response]
handler = URL_2_HANDLERS[environ['PATH_INFO']]
post_data = __parse_post_body(environ, ignore_get=False)
response = handler(post_data)
start_response('200 OK', [('Content-Type', 'application/json')])
return [str(response)]
def serve_forever():
WSGIServer((RISK_SERVER_HOST, RISK_SERVER_PORT), application).serve_forever()
if __name__ == "__main__":
serve_forever()