加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
risk_server.py 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
# coding=utf8
"""A http server which offers two uri: query and report"""
import cgi
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 = {}
content_type = environ["CONTENT_TYPE"] if "CONTENT_TYPE" in environ else None
if content_type is not None:
mimetype, options = cgi.parse_header(content_type)
# accept post json
if mimetype == "application/json" and environ["REQUEST_METHOD"] == "POST":
storage = environ['wsgi.input'].read()
if storage:
return json.loads(storage)
storage = FieldStorage(environ['wsgi.input'], environ=environ, keep_blank_values=True)
# 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.encode()]
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).encode()]
def serve_forever():
WSGIServer((RISK_SERVER_HOST, RISK_SERVER_PORT), application).serve_forever()
if __name__ == "__main__":
serve_forever()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化