Flask框架(一)
程序员文章站
2022-08-08 09:33:15
在run()中添加配置 debug 是否调试,修改后自动重启, 可以动态调试 threaded 是否多线程 post 端口 host 主机 插件、扩展库 1.下载,安装 2.初始化配置 flask-script 直接运行没有效果,需要输入参数 多个各种参数 Flask路由参数 请求方法 反向解析 ......
from flask import flask app = flask(__name__) @app.route('/') def index(): return '<h1>hello world</h1>' app.run()
在run()中添加配置
debug 是否调试,修改后自动重启, 可以动态调试
threaded 是否多线程
post 端口
host 主机
插件、扩展库
1.下载,安装
2.初始化配置
flask-script
from flask import flask from flask_script import manager app = flask(__name__) manager = manager(app=app) @app.route('/') def index(): a = 10 b = 0 c = a/10 return '<h1>hello world</h1>' if __name__=='__main__': # app.run(debug=true, port=8000, host='0.0.0.0') manager.run()
直接运行没有效果,需要输入参数
(venv) d:\python3\_flask>python hello.py runserver * serving flask app "hello" (lazy loading) * environment: production warning: do not use the development server in a production environment. use a production wsgi server instead. * debug mode: off * running on http://127.0.0.1:5000/ (press ctrl+c to quit)
多个各种参数
(venv) d:\python3\_flask>python hello.py runserver -d -r -h 0.0.0.0 -p 8000 * serving flask app "hello" (lazy loading) * environment: production warning: do not use the development server in a production environment. use a production wsgi server instead. * debug mode: on * restarting with stat * debugger is active! * debugger pin: 184-573-979 * running on http://0.0.0.0:8000/ (press ctrl+c to quit)
flask路由参数
@app.route('/params/<ni>/') def params(ni): return '获取参数' + ni @app.route('/get/<string:name>/') def get_name(name): return '获取name' + name @app.route('/get_age/<int:age>/') def get_age(age): return age #会将斜线认为是字符 @app.route('/get_path/<path:path>/') def get_path(path): return path @app.route('/get_uuid/<uuid:id>/') def get_uuid(id): return id.uuid64() #从列出的元组中的任意一个 @app.route('/get_any/<any(c, d, e):any>/') def get_any(any): return any
请求方法
@app.route('/get_any/<any(c, d, e):any>/', methods = ['get', 'post']) def get_any(any): return any
反向解析
@app.route('/url') def url(): print(url_for('get_any', any='c')) return '反向解析'