Python写接口api

Mr.Song2020年8月11日
小于 1 分钟

Python

写接口API
......

写接口API

from flask import Flask, request, jsonify

app = Flask(__name__)


@app.route('/testGet', methods=["GET"])
def calculateGet():
    a = request.args.get("a", 0)
    b = request.args.get("b", 0)
    c = int(a) + int(b)
    res = {"result": c}
    return jsonify(content_type='application/json;charset=utf-8',
                   reason='success',
                   charset='utf-8',
                   status='200',
                   content=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=8070)
评论
Powered by Waline v2.6.2