加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
risk_server.py 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
liu.hongwang 提交于 2019-06-14 17:34 . Add risk control project code
# 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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化