当前位置:网站首页>[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
边栏推荐
- Example of vim user automatic command
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- China nylon 11 industry research and future forecast report (2022 Edition)
- AI高考志愿填报:大厂神仙打架,考生付费围观
- 【Hot100】19. Delete the penultimate node of the linked list
- UML tourism management system "suggestions collection"
- How to use phpipam to manage IP addresses and subnets
- Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
- 【Hot100】20. Valid parentheses
- Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
猜你喜欢

Authentication processing in interface testing framework

Leetcode 77 combination -- backtracking method

Dataframe gets the number of words in the string

Motion capture system for apple picking robot

阿里云、追一科技抢滩对话式AI

How to use F1 to F12 correctly on laptop keyboard

VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...

PR basic clip operation / video export operation

巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...

MLPerf Training v2.0 榜单发布,在同等GPU配置下百度飞桨性能世界第一
随机推荐
Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
FPN network details
全面看待企业数字化转型的价值
Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
Problems encountered in IM instant messaging development to maintain heartbeat
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
Dataframe gets the number of words in the string
【Hot100】19. Delete the penultimate node of the linked list
How does go use symmetric encryption?
Kali install Nessus
Stonedb is building blocks for domestic databases, and the integrated real-time HTAP database based on MySQL is officially open source!
Basic use of MySQL
Research and investment strategy report of neutral protease industry in China (2022 Edition)
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
The sharp drop in electricity consumption in Guangdong shows that the substitution of high-tech industries for high-energy consumption industries has achieved preliminary results
How to cancel automatic search and install device drivers for laptops
How to optimize repeated if err in go language= Nil template code?
EndeavourOS移动硬盘安装
Authentication processing in interface testing 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 ” 了 .