当前位置:网站首页>[flask introduction series] cookies and session
[flask introduction series] cookies and session
2022-07-01 16:45:00 【Hall owner a Niu】
Personal profile
- Author's brief introduction : Hello everyone , I'm Daniel , New star creator of the whole stack .
- Blogger's personal website : A Niu's blog house
- Stand by me : give the thumbs-up + Collection ️+ Leaving a message.
- Series column :flask Framework quick start
- Maxim : To be light , Because there are people who are afraid of the dark !

Preface
Today I would like to sum up flask Medium cookie And session,flask Medium session And normal session It's different , For browsers session, Save in browser .
cookie
1. Set up cookie
# Import Flask Classes and request object
from flask import Flask,make_response
app = Flask(__name__)
# The default configuration loaded from the configuration object
class DefaultConfig(object):
SECRET_KEY = 'jstwn63bng'
DEBUG = True
ENV = 'development'
app.config.from_object(DefaultConfig)
@app.route('/cookie')
def cookie():
resp = make_response(" Set up cookie")
resp.set_cookie("aniu","handsome")
return resp
# Flask Application's run Method start up web The server
if __name__ == '__main__':
app.run(port=8000)

2. Set up cookie The period of validity
@app.route('/cookie')
def cookie():
resp = make_response(" Set up cookie")
resp.set_cookie("aniu","handsome",max_age=4000) #max_age Is the number of milliseconds of the maximum expiration time
return resp
3. obtain cookie
# Import Flask Classes and request object
from flask import Flask,make_response,request
app = Flask(__name__)
# The default configuration loaded from the configuration object
class DefaultConfig(object):
SECRET_KEY = 'jstwn63bng'
DEBUG = True
ENV = 'development'
app.config.from_object(DefaultConfig)
# Set up cookie
@app.route('/set_cookie')
def set_cookie():
resp = make_response(" Set up cookie")
resp.set_cookie("aniu","handsome")
return resp
# obtain cookie
@app.route('/get_cookie')
def get_cookie():
resp = request.cookies.get('aniu')
return resp
# Flask Application's run Method start up web The server
if __name__ == '__main__':
app.run(port=5000)

4. Delete cookie
@app.route('/delete_cookie')
def delete_cookie():
resp = make_response(" Set up cookie")
resp.delete_cookie("aniu","handsome")
return resp
session
Be careful : You have to set it SECRET_KEY, Why? ? We said in the preface ,flask Medium session It's a browser session, Save in browser , So you need a key .
# Import Flask Classes and request object
from flask import Flask,session
app = Flask(__name__)
# The default configuration loaded from the configuration object
class DefaultConfig(object):
SECRET_KEY = 'jstwn63bng'
DEBUG = True
ENV = 'development'
app.config.from_object(DefaultConfig)
# Set up session
@app.route('/set_session')
def set_session():
session['username'] = 'aniu'
return 'set session ok'
# Read
@app.route('/get_session')
def get_session():
username = session.get('username')
return 'get session username {}'.format(username)


reflection :flask take session Where did you save it ?
stay django in , about session The preservation of the , There will be a special table in the database to save session data , And we flask Medium session It was said that , It's actually a browser session, He saves it in the browser .
> therefore , There will be security problems after putting it in the browser , Therefore, we must add one “ Signature ”, Only myself “ Signature ” Talent , You can't use it after taking it and modifying it . How can I guarantee that this signature is mine , Can't live without our SECRET_KEY 了 . So what you see session The content in can be said to be “ Be encrypted ” 了 .
Conclusion
If you think the blogger's writing is good , You can pay attention to the current column , Bloggers will finish this series ! You are also welcome to subscribe to other good columns of bloggers .
Series column
Soft grinding css
Hard bubble javascript
The front end is practical and small demo
For more columns, please go to the blogger's home page ! Bloggers are also very interesting , You can patronize : A Niu's blog house
边栏推荐
- How to solve the keyboard key failure of notebook computer
- Rhcsa Road
- SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
- Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
- Preliminary study on golang crawler framework
- MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
- Rhcsa Road
- sql刷题627. 变更性别
- What is the effect of choosing game shield safely in the game industry?
- How to use MySQL language for row and column devices?
猜你喜欢

Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)

【Hot100】20. Valid parentheses

PostgreSQL 存储结构浅析

Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation

免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!

How to solve the keyboard key failure of notebook computer

Submission lottery - light application server essay solicitation activity (may) award announcement

接口测试框架中的鉴权处理

Motion capture system for apple picking robot

今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
随机推荐
Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
Buuctf gold III
String类
Kali install Nessus
【Hot100】19. Delete the penultimate node of the linked list
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
怎么用MySQL语言进行行列装置?
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
The supply of chips has turned to excess, and the daily output of Chinese chips has increased to 1billion, which will make it more difficult for foreign chips
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
Submission lottery - light application server essay solicitation activity (may) award announcement
UML旅游管理系统「建议收藏」
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
vim用户自动命令示例
Principle of SSM framework
> therefore , There will be security problems after putting it in the browser , Therefore, we must add one “ Signature ”, Only myself “ Signature ” Talent , You can't use it after taking it and modifying it . How can I guarantee that this signature is mine , Can't live without our SECRET_KEY 了 . So what you see session The content in can be said to be “ Be encrypted ” 了 .