使用python编写简单的api接口
首先先安装flask模块
pip install Flask
接下来编写py文件
''' Author : 智昕 Date : 2022-06-10 10:25:12 ''' from flask import Flask, request, jsonify app = Flask(__name__) @app.route('/testGet', methods=["GET"]) def calculateGet(): print(request) a = request.args.get("a", 0) b = request.args.get("b", 0) c = int(a) + int(b) res = {"result": c} return jsonify(code=0,data=res) @app.route('/testPost', methods=["POST"]) def calculatePost(): params = request.form if request.form else request.json print(params) a = params.get("a", 0) b = params.get("b", 0) c = a + b res = {"result": c} return jsonify(content_type='application/json;charset=utf-8', reason='success', charset='utf-8', status='200', content=res) if __name__ == '__main__': app.run(host='0.0.0.0', threaded=True, debug=False, port=8080)
执行py文件
访问测试:http://127.0.0.1:8070/testGet?a=12&b=1231223