当前位置:网站首页>cookie增删改查和异常
cookie增删改查和异常
2022-07-27 05:02:00 【pink_Pig___】
cookie的增删改查
from flask import Flask, make_response, request
app = Flask(__name__)
# 添加cookie
@app.route('/set_cookie')
def set_cookie():
resp = make_response('设置cookie')
resp.set_cookie('name', 'zhangsan', max_age=60) # 设置cookie
return resp
# 修改cookie,也就是重置value
@app.route('/put_cookie')
def set_cookie():
resp = make_response('修改cookie')
resp.set_cookie('name', 'wangwu', max_age=60) # 设置cookie
return resp
# 获取cookie
@app.route('/get_cookie')
def get_cookie():
name = request.cookies.get('name')
print(name)
resp = make_response('获取cookie')
return resp
# 删除cookie
@app.route('/del_cookie')
def del_cookie():
resp = make_response('删除cookie')
resp.delete_cookie('name')
return resp
if __name__ == '__main__':
app.run()
session的曾删改查
- 需要制作一个包,写入密钥
# 值可以随便写
SECRET_KEY = 'ajfijfakjfg'
from flask import Flask, make_response, session
app = Flask(__name__)
# 调用密钥
app.config.from_pyfile(settings.py)
# 添加session
@app.route('/set_session')
def set_session():
session['password'] = 123456
return 'session设置成功'
# 修改session,也是重置value
@app.route('/set_session')
def set_session():
session['password'] = 11111
return 'session修改成功'
# 获取session
@app.route('/get_session')
def get_session():
pwd = session.get('password')
return f'获取到的key:{
pwd}'
# 删除session
@app.route('/del_session')
def del_session():
resp = make_response('删除session')
resp.delete_cookie('session')
return resp
if __name__ == '__main__':
print(app.config)
app.run()
抛出异常
from flask import Flask, abort
app = Flask(__name__)
@app.route('/error')
def error():
abort(404)
# 捕获异常
@app.errorhandler(404)
def get_error(e):
return '出错了'
if __name__ == '__main__':
app.run()
构造方法
@app.route('/page')
def page():
print('页面执行')
return '页面执行'
@app.before_first_request
def first():
print('页面请求前执行,只请求一次')
@app.before_request
def before():
print('每次执行')
@app.after_request
def after(resp):
print(resp)
resp.status = 404
print('页面处理请求结果报错,可能不会在执行')
return resp
@app.teardown_request
def tr(e):
print('页面处理请求后执行,报错也会执行')
if __name__ == '__main__':
app.run()
边栏推荐
- JVM part I: memory and garbage collection part II -- class loading subsystem
- I've heard the most self disciplined sentence: those I can't say are silent
- Constraints of MySQL table
- B1024 科学计数法
- 李宏毅机器学习组队学习打卡活动day06---卷积神经网络
- B1021 个位数统计
- How idea creates a groovy project (explain in detail with pictures and texts)
- 枚举类实现单例模式
- Interface and abstract class / method learning demo
- 2021 OWASP top 5: security configuration error
猜你喜欢
随机推荐
File processing (IO)
Idea remote debugging
B1028 census
Domestic mainstream ERP software market
B1028 人口普查
Shell course summary
Quoted popular explanation
如何查看导师的评价
Sunyanfang, co-founder of WeiMiao: take compliance as the first essence and become the "regular army" of financial and business education
Differences among left join, inner join and right join
Notes series k8s orchestration MySQL container - stateful container creation process
Typescript details
ERROR! MySQL is not running, but PID file exists
mq常见问题
Detailed description of binary search tree
idea远程调试debug
[CSAPP] Application of bit vectors | encoding and byte ordering
Raspberry pie RTMP streaming local camera image
李宏毅机器学习组队学习打卡活动day06---卷积神经网络
Detailed explanation of pointer constant and constant pointer
![[acwing] solution to the 61st weekly match](/img/31/765f4ce9f779e8093668e7606e0198.png)








