• 基于Flask的 api(一)


    Flask是一个基于Python开发的微型web框架

    使用Flask实现简单的 api

    安装

     pip install flask

    flask版本

    import flask
    print(flask.__version__)

    $ python test.py
    1.1.2

    eg:

    from flask import Flask
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    
    if __name__ == "__main__":
        app.run()

    浏览器打开http://127.0.0.1:5000/

    页面输出

      hello,world

    说明:

      Flask自带的Server默认监听端口为5000

    启用了 Flask 调试 模式

    from flask import Flask
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    if __name__ == "__main__":
        app.run(debug=True)

    修改端口

    from flask import Flask
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    if __name__ == "__main__":
        app.run(debug=True,port=6000)

    1.GET方法

    参数形式 ?key=value

    from flask import Flask,request,abort
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    @app.route("/testGet", methods=['GET'])
    def testGet():
        name = request.args.get('name','')
        age = request.args.get('age','')
        return {'name':name,'age':age}
    
    if __name__ == "__main__":
        app.run()

    postman测试

    使用 path 的形式

    from flask import Flask,request,abort
    import os
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    @app.route("/testGet/<name>", methods=['GET'])
    def testGet(name,age=20):
    
        return {'name':name,'age':age}
    
    if __name__ == "__main__":
        app.run(debug=True,port=5000)

    使用postman测试

    from flask import Flask,request,abort
    import os
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    @app.route("/testGet/<name>/<age>", methods=['GET'])
    def testGet(name,age):
    
        return {'name':name,'age':age}
    
    if __name__ == "__main__":
        app.run(debug=True,port=6000)

    2.POST方法

    (1)接收json数据

    from flask import Flask,request,abort
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    @app.route("/testPost", methods=['POST'])
    def testPost():
        if not request.json or not 'text' in request.json:
            abort(400)
        text = request.json.get('text', '')
        return {'text':text}
    
    if __name__ == "__main__":
        app.run()

    postman测试

     (2)formdata数据,request.form

    from flask import Flask,request,abort
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    @app.route('/testPost', methods=['POST'])
    def testPost():
        text = request.form['text']
        print(text)
        return {'text':text}
    
    if __name__ == "__main__":
        app.run(debug=True,port=5000)

      eg:文件上传并保存

    from flask import Flask,request,abort
    import os
    
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'hello,world'
    
    @app.route('/testPost', methods=['POST'])
    def testPost():
        text = request.form['text']
        message = '没有文件'
        success = False;
        if request.files:
            file = request.files['file']
            if file:
                success = True
                filename = file.filename
                file.save(os.path.join(app.root_path, 'upload', filename))
                message = '文件已保存'+os.path.join(app.root_path, 'upload', filename)
        print(text)
        return {'success':success,'text':text,'message':message}
    
    if __name__ == "__main__":
        app.run(debug=True,port=5000)

    postman测试

  • 相关阅读:
    第二阶段第九天
    第二阶段第八天
    一轮项目冲刺——移山小分队(3)
    一轮项目冲刺——移山小分队(2)
    典型用户和用户场景
    一轮项目冲刺——移山小分队
    寻找水王
    NABCD需求分析
    每日记录01
    二维数组最大连通和的求解
  • 原文地址:https://www.cnblogs.com/baby123/p/14023538.html
Copyright © 2020-2023  润新知