当前位置:网站首页>[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
边栏推荐
- UML旅游管理系统「建议收藏」
- 数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
- C語言輸入/輸出流和文件操作
- 剑指 Offer II 015. 字符串中的所有变位词
- 制造业数字化转型究竟是什么
- 虚拟串口模拟器和串口调试助手使用教程「建议收藏」
- Detailed explanation of activity life cycle and startup mode
- Redis6.0 新功能
- Hi Fun Summer, play SQL planner with starrocks!
- Principle of motion capture system
猜你喜欢

Are you still using charged document management tools? I have a better choice! Completely free
![[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment](/img/0e/52e37527bc717c7a55741725087bad.png)
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment

【观察】数字化时代的咨询往何处走?软通咨询的思与行

软件工程导论——第六章——详细设计

Guide for high-end programmers to fish at work

嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!

VMware 虚拟机启动时出现故障:VMware Workstation 与 Hyper-v 不兼容...

VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v

模板引擎Velocity 基础

Authentication processing in interface testing framework
随机推荐
Rhcsa Road
[nodemon] app crashed - waiting for file changes before starting... resolvent
What are the differences between PHP and DW
AI高考志愿填报:大厂神仙打架,考生付费围观
【Hot100】17. Letter combination of telephone number
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
P2893 [usaco08feb] making the grade g (DP & priority queue)
How to use MySQL language for row and column devices?
Go 语言怎么使用对称加密?
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
怎么用MySQL语言进行行列装置?
vim用户自动命令示例
Problems encountered in IM instant messaging development to maintain heartbeat
FPN网络详解
Golang爬虫框架初探
Zabbix2.2 monitoring system and application log monitoring alarm
Go 语言源码级调试器 Delve
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
> 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 ” 了 .