当前位置:网站首页>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')
三:在应用对象上注册蓝图对象
边栏推荐
- NLP (natural language processing) natural language processing learning
- 利用模m的原根存在性判断以及求解
- Super nice navigation page (static page)
- Foundry tutorial: writing scalable smart contracts in various ways (Part 1)
- Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)
- Status management bloc provider geTx
- 2022年收益率最高的理财产品是哪个?
- 23.9 application exit application exit
- Xtrabackup appears' flush no '_ WRITE_ TO_ BINLOG TABLES‘: 1205 (HY000) Lock wait timeout exceeded;
- CV learning notes (optical flow)
猜你喜欢

MySQL 8.0 OCP (1z0-908) has a Chinese exam

Spark scheduling analysis

OA项目之我的会议(查询)

Winter vacation homework & Stamp cutting

Excel find duplicate lines

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

Neo eco technology monthly | help developers play smart contracts

2022-7-9 personal qualifying 6 competition experience

How to safely delete a useless activity in Android studio

为什么要在时钟输出上预留电容的工位?
随机推荐
Bee guitar score high octave and low octave
Kotlin function
Oracle 19C OCP 1z0-082 certification examination question bank 1
Oracle 19C OCP 1z0-082 certification examination question bank (13-18)
Uninstallation of dual systems
Write common API tools swagger and redoc
Kotlin program control
Mysql database connection / query index and other common syntax
22-07-14 personal training match 2 competition experience
Storage of drawings (refined version)
Flutter custom player progress bar
Oracle 19C OCP 1z0-082 certification examination question bank (24-29)
OSPF summary
Number of briquettes & Birthday Candles & building blocks
12306 ticket system crawling - 1. Saving and reading of city code data
MySQL 8.0 OCP 1z0-908 certification examination question bank 1
【搜索专题】看完必会的搜索问题之洪水覆盖
Alphabetic string
Lesson 3: gcc compiler
Super nice navigation page (static page)