当前位置:网站首页>[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
边栏推荐
- 模板引擎Velocity 基础
- Rhcsa Road
- Kali install Nessus
- C language input / output stream and file operation
- 游戏行业安全选择游戏盾,效果怎么样?
- Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
- 程序员职业生涯真的很短吗?
- Authentication processing in interface testing framework
- Go 语言怎么使用对称加密?
- How to optimize repeated if err in go language= Nil template code?
猜你喜欢
Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
C language input / output stream and file operation
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
What is the digital transformation of manufacturing industry
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
怎么用MySQL语言进行行列装置?
String类
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
随机推荐
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
Principle of SSM framework
Dataframe gets the number of words in the string
Comprehensively view the value of enterprise digital transformation
Submission lottery - light application server essay solicitation activity (may) award announcement
想做软件测试的女孩子看这里
P2893 [usaco08feb] making the grade g (DP & priority queue)
Germany if was crowned with many awards. How strong is this pair of headphones? In depth evaluation of yinpo GTW 270 hybrid
P2592 [zjoi2008] birthday party (DP)
Template Engine Velocity Foundation
How to cancel automatic search and install device drivers for laptops
Zabbix2.2 monitoring system and application log monitoring alarm
[nodemon] app crashed - waiting for file changes before starting...解决方法
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
Problems encountered in IM instant messaging development to maintain heartbeat
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
How to restore the system with one click on Lenovo laptop
Installation and use of sqoop
怎么用MySQL语言进行行列装置?
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022