美文网首页
python写简单的api接口

python写简单的api接口

作者: yichen_china | 来源:发表于2021-03-05 15:42 被阅读0次
    #!D:\Programs\Python\Python37
    # -*- coding:utf-8 -*-
    # @Author : Echo
    # @Time :2021/3/5 15:35
    # from flask import Flask, request, jsonify
    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(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)
    

    http://0.0.0.0:8070/testGet?a=12&b=43

    相关文章

      网友评论

          本文标题:python写简单的api接口

          本文链接:https://www.haomeiwen.com/subject/ntorqltx.html