以下代码可以作为测试接收请求的程序直接复制使用:
from flask import flask, request
app = flask(__name__)
@app.route('/service', methods=['post'])
def post_route():
if request.method == 'post':
headers = request.headers
data = request.get_json()
print'headers:',headers
print('data received: "{data}"'.format(data=data))
return "request processed.\n"
app.run()
请求命令如下
curl -h "content-type:application/json" -h "data_type:msg" -x post --data '{"dmac": "00:0c:29:ea:39:70", "alert_type": "alarm", "risk": 2, "trojan_name": "trojan.qq3344", "smac": "00:0c:29:ea:39:66", "sub_alert_type": "trojan", "sport": 11, "id": "153189767146", "desc": "nosecure 1.2 \u6728\u9a6c\u53d8\u79cd4\u8fde\u63a5\u64cd\u4f5c", "sip": "62.4.07.18", "dip": "139.82.31.91", "rule_id": 123451, "trojan_type": 4, "time": "2018-07-18 15:07:51", "dport": 61621, "detector_id": "170301020011", "os": "windows", "trojan_id": 50030}' http://127.0.0.1:5000/service