Flask启动与配置
项目启动
from flask import Flask
app = Flask(__name__);
app.run()
路由配置
@app.route()
另一种路由注册方式
app.add_url_rule("/hello",view_func= hello) -> 实际上注解式注册路由最终会调用该种注册方式
app.run 相关参数
热更新 使用 DEBUG 模式 app.run(debug= True)
指定访问的ip地址 如若不指定则只有 localhost/ 192.0.0.1能访问web服务 app.run(host = '0.0.0.0',debug= True)
指定端口号 app.run(host = '0.0.0.0',debug= True , prot=81)
配置文件
app.config.from_object(“模块路径") 加载配置文件
app.config["字段"] 调用
注: 字段为大写字母 小写会被自动忽略
if __name__ == __main__ 的使用默认调用 flask 自带ed服务器,但生产环境会使用 nginx+uwsgi
responce
视图函数返回 包含 status code content-type
content-type 默认为 text/html
使用 make_responce返回 responce 注 状态码为 301为转发,在headers中使用localhost 重定向到其他地址
responce = make_responce(data, status code)
headers = { 'content-type' : 'application/json' }
responce.headers = headers
return responce
可以用 return data, status code, headers
记录一个问题 : 启动方法在一个包内,配置文件在另一个包内 ,app.config.from_object(“模块路径") 模块路径是指?

浙公网安备 33010602011771号