基于Flask的 api(一)

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

使用Flask实现简单的 api

安装

pip install flask

flask版本

import flaskprint(flask.__version__)$ python test.py1.1.2

eg:

from flask import Flaskapp = 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 Flaskapp = Flask(__name__)@app.route('/')def hello():
    return 'hello,world'if __name__ == "__main__":
    app.run(debug=True)

修改端口

from flask import Flaskapp = 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,abortapp = 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,abortimport osapp = 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,abortimport osapp = 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,abortapp = 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,abortapp = 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,abortimport osapp = 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测试

(0)

相关推荐