当前位置:网站首页>03异常处理,状态保持,请求钩子---04大型项目结构与蓝图
03异常处理,状态保持,请求钩子---04大型项目结构与蓝图
2022-07-26 08:38:00 【糖果ᨐ不甜丫꧂】
1.session和cookie的区别
cookie的缺点:可以串改数据,客户可以随意串改
解决:cookie通过session客户端进行修改,并且是加密的
2.Cookie的实现
2.1.cookie的增加
from flask import Flask,response
app=Flask(__name__) # 实例化数据
# 定义一个函数
@app.route('/set_cookie')
def set_cookie():
# 响应对象 make_response 返回的值
resp=make_response('设置cookie')
# 设置cookie max_age cookie的有效期
resp.set_cookie('name','zhangsan',max_age=60)
# 响应返回
return resp
if __name__=='__main__':
app.run()
结果:
显示有效期的时间等:我设置的是60s
2.2.cookie的查
from flask import Flask
app=Flask(__name__)
# 定义一个路由
@app.route('/get_cookie')
def get_cookie():
name=request.cookies.get('name')
resp=make_response('获取cookie')
return resp
# 执行
if __name__='__main__':
app.run()
2.3cookie的删除
# 导入flask包
from flask import Flask
# 实例化一下
app=Flask(__name__)
# 定义路由
@app.route('/del_cookie')
# 定义一个cookie的函数
def del_cookie():
resp=make_response('删除cookie')
resp.del_cookie('name')
return resp
if __name__='__main__':
app.run()
1.蓝图的介绍和使用
蓝图的概念:
蓝图是一种扩展已有Flask应用结构的方法,并且提供易总功能类似的视图组合在一起的方式,简单来说就相当于与空调的遥控器
蓝图的步骤(三步):
一:创建一个蓝图对象
二:在创建好的蓝图对象进行操作
from flask import Flask,Blueprint,jsonify
from flask_restful import Api,Resource,reqparse
from models.model import *
user_db=Blueprint('user_db',__name__,url_prefix='/user')
api=Api(user_db)
class HomeView(Resource):
def get(self):
...
def post(self):
...
def put(self):
...
def delete(self):
...
api.add_resource(HomeView,'/home')
三:在应用对象上注册蓝图对象
边栏推荐
- 2022-7-6 personal qualifying 3 competition experience
- Pxe原理和概念
- Install HR schema, example, and Scott schema on Oracle and MySQL
- Add in the registry right click to open in vscode
- 22-07-14 personal training match 2 competition experience
- Flutter distribution
- Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)
- CV learning notes (optical flow)
- 1、 Redis data structure
- shell编程
猜你喜欢

Super potential public chain dfinity -- the best time for DFI developers to enter

【C语言】程序员筑基功法——《函数栈帧的创建与销毁》

基于Raft共识协议的KV数据库

Memory management based on C language - Simulation of dynamic partition allocation

Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)

OA项目之我的会议(查询)
![[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls](/img/8b/d5792a00fc5798d3d960f1f1b1088e.png)
[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls

Vscode utility shortcut

Write common API tools swagger and redoc
![[GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)](/img/25/475c91d7e673fda3930e5a69be0f28.png)
[GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)
随机推荐
2022-7-5 personal qualifying 2 competition experience
Which financial product has the highest yield in 2022?
P3743 kotori的设备
Oracle 19C OCP 1z0-082 certification examination question bank (42-50)
Does flinkcdc now support sqlserver instance name connection?
2022-7-7 personal qualifying 4 competition experience
QT note 2
Kotlin data type
Huffman transformation software based on C language
2022年全国职业院校技能大赛“网络安全”竞赛试题文件上传渗透测试答案Flag
Using the primitive root of module m to judge and solve
[C language] programmer's basic skill method - "creation and destruction of function stack frames"
Oracle 19C OCP 1z0-082 certification examination question bank (7-12)
Oracle 19C OCP 1z0-082 certification examination question bank (51-60)
基于Raft共识协议的KV数据库
Install HR schema, example, and Scott schema on Oracle and MySQL
Maximum common substring & regularity problem
Super potential public chain dfinity -- the best time for DFI developers to enter
Oracle 19C OCP 1z0-082 certification examination question bank (24-29)
基于C语言实现的人机交互软件