大家好呀 ~ 我是Willis,今天要介紹的是Cookie喔 ! Σ>―(〃°ω°〃)♡→
網站為了辨別使用者身分而儲存在用戶端(Client Side)上的資料,使用Cookie能夠讓使用者在網路瀏覽上更加方便,但在網路隱私的方面來說Cookie危害了使用者的安全。
這裡的Cookie不是餅乾喔 ! ┐(´д`)┌
make_response( )
在設置Cookie,我們先來學習如何設置回應。
app.py
# 導入模組 Flask, render_template, make_response
from flask import Flask, render_template, make_response
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route('/str') # 回傳字串
def str():
response = make_response("Hello World!")
return response
@app.route('/index') # 回傳網頁
def index():
response = make_response(render_template("login.html"))
return response
if __name__ == '__main__':
app.run()
/str 路徑頁面
/index 路徑頁面
set_cookie
set_cookie 設定
app.py
from flask import Flask, make_response # 導入模組 Flask, make_response
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/cookie") # 製造cookie
def setcookie():
response = make_response('My cookie')
response.set_cookie(key='My_cookie', value='Hello')
return response
if __name__ == '__main__':
app.run()
request.cookies.get()
app.py
from flask import Flask, make_response, request # 導入模組 Flask, make_response
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/cookie") # 製造cookie
def setcookie():
response = make_response('My cookie')
response.set_cookie(key='My_cookie', value='Hello')
return response
@app.route("/get") # 取得cookie
def getcookie():
get_cookie = request.cookies.get('My_cookie')
return get_cookie
if __name__ == '__main__':
app.run()
app.py
from flask import Flask, make_response, request # 導入模組 Flask, make_response
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/cookie") # 製造cookie
def setcookie():
response = make_response('My cookie')
response.set_cookie(key='My_cookie', value='Hello')
return response
@app.route('/del') # 刪除cookie
def delcookie():
response = make_response('Delete cookies')
response.set_cookie(key='My_cookie', value='', expires=0)
return response
if __name__ == '__main__':
app.run()
/cookie 製造cookie
/del 刪除cookie
https://www.maxlist.xyz/2019/05/11/flask-cookie/
下一章節我會說Session,今天就到這裡囉 ~ 掰掰~ (๑•̀ω•́)ノ