打开python服务
设置postman如下,并发送:
- postman新建请求
- 设置请求方式为post
- 设置地址、raw、json方式、内容如下
结果:
python如下:
from flask import Flask, request, jsonify
app = Flask(__name__) # 实例化对象
@app.route('/test/stats/', methods=["POST", "GET"])
def display():
try:
print('request method:', request.method)
if request.method == "POST":
data = request.get_json() # 传入的数据
print("data:", data)
get_id = data.get("id")
get_seconds = int(data.get("seconds"))
if get_id is None or get_seconds is None:
return jsonify(msg="缺少参数")
elif get_id == '500' and get_seconds > 240:
r = {'flag': '1'} # 假设这是你的字典
# 检查键 'flag' 是否存在
if 'flag' in r:
print(r['flag']) # 如果键存在,则打印对应的值
return jsonify(r)
else:
print("键 'flag' 不存在于字典中。") # 如果键不存在,则打印错误消息
return jsonify({'flag': '0'})
else:
return jsonify({'error': 'Invalid data'})
elif request.method == "GET":
return "Hello World!"
except Exception as e:
print(e)
return jsonify(msg="出错了,请查看是否正确访问")
if __name__ == '__main__':
# app.run() # 默认本主机访问http://127.0.0.1:5000/test/first_post
# app.run(host="0.0.0.0") # 任何主机都可以访问http://10.10.33.229:5000/test/first_post
app.run(port='5012')