工作中别的同事经常需要用到自己写的程序,记录一下python flask打包最简单接口:
from flask import Flask,request,jsonify
app = Flask(__name__)
@app.route('/',methods = ['POST'])
def apistart():
try:
# 从网页获取json数据
data = request.get_json(force=True)
# 返回
return jsonify({"code": 200, "msg": "success", "res": data}), 200
except Exception as e:
raise e
if __name__ == '__main__':
app.run(port=5000)
启动后默认url为127.0.0.1:5000,使用requests库传递参数。