flask+pyinstaller实现mock接口,并打包到exe运行使用postman验证

时间:2024-02-15 14:34:54
from flask import Flask, request, jsonify app = Flask(__name__) @app.route("/login", methods=['POST']) def login(): username = request.json.get("username").strip() # 用户名 password = request.json.get("password").strip() # 密码 if username and password: if len(username) <= 2: return jsonify({"code": 999, "msg": "用户名不能少于2位"}) elif len(password) <= 6: return jsonify({"code": 999, "msg": "密码不能少于6位"}) else: return jsonify({"code": 200, "msg": "请求成功", 'token': 'ey66666', 'user_balance': 2000}) else: return jsonify({"code": 999, "msg": "用户名/密码不能为空,请检查"}) @app.route("/goods_info", methods=['GET']) def goods_info(): token = request.headers.get("token") if token != 'ey66666': return jsonify({"code": 999, "msg": "请登录后重试"}) return jsonify({'goods_id': 1, 'goods_inventory': 99, 'goods_price': 888.9, 'goods_notes': '这是一个虚假的货物'}) @app.route("/purchase", methods=['POST']) def purchase(): token = request.headers.get("token") if token != 'ey66666': return jsonify({"code": 999, "msg": "请登录后重试"}) purchase_quantity = request.json.get("purchase_quantity") # 购买数量 goods_id = request.json.get("goods_id") # 商品id user_balance = request.json.get("user_balance") # 用户余额 print(purchase_quantity,goods_id,user_balance) if goods_id == 1: if 99 < purchase_quantity: return jsonify({"code": 999, "msg": "商品库存不足"}) elif 888.9 * purchase_quantity > user_balance: return jsonify({"code": 999, "msg": "余额不足"}) return jsonify({"code": 200, "msg": "购买成功", 'goods_id': goods_id}) else: return jsonify({"code": 999, "msg": "商品不存在"}) if __name__ == '__main__': app.run(host='0.0.0.0', port=7979 , debug=True)