Flask框架Flask是一款轻量级的web应用框架,最便捷的falsk web应用甚至只需要安装flask、再加上python环境就能实现前后端交互。它提供了比较方便的注解的使用、因为我是java出身对于注解的使用也是比较喜欢,不需要繁杂的框架搭建过程几行代码就能实现访问后端接口。 相关依赖库 1''' 2相关依赖库 3''' 4# 安装flask库 5 6# pip install flask 7 8# 导入Falsk对象 9 10from flask import Flask 11 12# 导入json数据处理函数 13 14from flask import jsonify 15 16# 获取app处理对象 17 18app = Flask(__name__)
接口路由 1# 函数绑定路由 2 3@app.route('/hello_world') 4 5def hello_world(): 6 7 """ 8 :return:返回字符串 9 """ 10 11 return '<h4>第一个Flask应用</h4>' 12 13 14@app.route('/get_data') 15 16def get_data(): 17 18 """ 19 :return:返回字符串 20 """ 21 22 return '<span>数据应用</span>' 23 24@app.route('/get_json_data') 25 26def get_json_data(): 27 28 """ 29 :return:返回json数据对象 30 """ 31 32 return jsonify(data={'msg': 'hello_world'})
服务运行1if __name__ == '__main__': 2 3 """ 4 run()函数启动web服务 5 """ 6 7 app.run(debug=True)
交互日志 1''' 2交互日志 3''' 4 5# * Serving Flask app 'main' (lazy loading) 6# * Environment: production 7# WARNING: This is a development server. Do not use it in a production deployment. 8# Use a production WSGI server instead. 9# * Debug mode: on 10# * Restarting with stat 11# * Debugger is active! 12# * Debugger PIN: 701-063-048 13# * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
|