当前位置:网站首页>(09) flask is OK if it has hands - cookies and sessions
(09) flask is OK if it has hands - cookies and sessions
2022-07-24 14:56:00 【Free little ice】
zero 、 Preface
This article , Elaborate on Flask in Cookie and Session

- Why Cookie and Session Well ?
answer : because http Request is stateless , How do you understand that ? When you visit B When standing , If you don't Cookie perhaps Session,B The station thinks you are an unlisted user . If you have Cookie or Session, that B The station will know that you are logged in , And know who you are . So I can return the information related to you- The difference between the two :
answer :Cookie It's plain text ,Session It's encrypted. Cookie.
Cookie Use
from flask import Flask, Response, request
app = Flask(__name__)
@app.route("/set_cookie")
def set_cookie():
response = Response("set cookie success")
response.set_cookie("user_id","123")
return response
@app.route("/get_cookie")
def get_cookie():
cookies = request.cookies
if cookies.get("user_id") == "123":
return " Dear users, hello , Yours cookies yes " + str(cookies)
else:
return " You are not VIP, Please get your identity first "
if __name__ == "__main__":
app.run(debug=True,port=5000)
- First visit
/homeAddress , You will find that you have no Cookie Give you back aYou are not VIP, Please get your identity first - But when we first visit
/set_cookie, We'll get onecookie, Then we are visiting/homeAddress , You can successfully display Dear user, hello
Session Use
from flask import Flask, session
app = Flask(__name__)
# !!! You need to set up a SECRET_KEY Used to encrypt Cookie
app.config["SECRET_KEY"] = "123"
@app.route("/set_session")
def set_session():
session["user_name"] = "zhuxiaoxiao"
return " Set up session success " + session["user_name"]
@app.route("/home")
def home():
if session.get("user_name") == "zhuxiaoxiao":
return " Dear users , Login successful "
else:
return " Please login first ?"
if __name__ == "__main__":
app.run(debug=True,port=5000)
- Session The way of use is overall and Cookie It's the same .
- It should be noted that the Session front , It must be set
SECRET_KEY, Used to encrypt Cookie

边栏推荐
- Deep learning 1 perceptron and implementation of simple back propagation network
- Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
- Activate the newly installed Anaconda in the server
- Overall testing framework for performance testing
- Meaning of 7 parameters of thread pool
- Grpc middleware implements grpc call retry
- Similarities and differences between nor flash and NAND flash
- spark:指定日期输出相应日期的日志(入门级-简单实现)
- Leetcode · daily question · 1184. distance between bus stops · simulation
- Detailed explanation of document operation
猜你喜欢
![Property datasource is required exception handling [idea]](/img/f3/50d36ed9445695c02e2bd622109d66.png)
Property datasource is required exception handling [idea]

Decrypt "sea Lotus" organization (domain control detection and defense)
![Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布](/img/1e/27f107d514ded6641410cc5a45764b.png)
Rasa 3.x 学习系列-Rasa [3.2.4] - 2022-07-21 新版本发布

Spark: get the access volume of each time period in the log (entry level - simple implementation)

使用 Fiddler Hook 报错:502 Fiddler - Connection Failed

Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration

Explain the edge cloud in simple terms | 2. architecture

Strongly connected component

Maotai ice cream "bucked the trend" and became popular, but its cross-border meaning was not "selling ice cream"

Learning and thinking about the relevant knowledge in the direction of building network security knowledge base
随机推荐
Number of bytes occupied by variables of type char short int in memory
Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
Caffe framework and production data source for deep learning
CSDN垃圾的没有底线!
onBlur和onChange冲突解决方法
(零九)Flask有手就行——Cookie和Session
The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic
佣金哪家券商最低,我要开户,手机上开户安不安全
Native asynchronous network communication executes faster than synchronous instructions
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
Sword finger offer II 001. integer division
XOR procedure
股票开户之后就可以购买6%的理财产品了?
Tiger mouth waterfall: Tongliang version of xiaohukou waterfall
Leetcode-09 (next rank + happy number + full rank)
使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
关于构建网络安全知识库方向相关知识的学习和思考
Maotai ice cream "bucked the trend" and became popular, but its cross-border meaning was not "selling ice cream"
Must use destructuring props assignmenteslint
Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration