W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
與Cookie不同,Session(會(huì)話)數(shù)據(jù)存儲(chǔ)在服務(wù)器上。會(huì)話是客戶端登錄到服務(wù)器并注銷服務(wù)器的時(shí)間間隔。需要在該會(huì)話中保存的數(shù)據(jù)會(huì)存儲(chǔ)在服務(wù)器上的臨時(shí)目錄中。
為每個(gè)客戶端的會(huì)話分配會(huì)話ID。會(huì)話數(shù)據(jù)存儲(chǔ)在cookie的頂部,服務(wù)器以加密方式對(duì)其進(jìn)行簽名。對(duì)于此加密,F(xiàn)lask應(yīng)用程序需要一個(gè)定義的SECRET_KEY。
Session對(duì)象也是一個(gè)字典對(duì)象,包含會(huì)話變量和關(guān)聯(lián)值的鍵值對(duì)。
例如,要設(shè)置一個(gè)'username'會(huì)話變量,請(qǐng)使用以下語句:
Session['username'] = 'admin'
要釋放會(huì)話變量,請(qǐng)使用pop()方法。
session.pop('username', None)
以下代碼是Flask中的會(huì)話工作的簡單演示。URL '/'只是提示用戶登錄,因?yàn)槲丛O(shè)置會(huì)話變量'username'。
@app.route('/')
def index():
if 'username' in session:
username = session['username']
return 'Logged in as ' + username + '<br>' \
"<b><a href = '/logout'>click here to log out</a></b>"
return "You are not logged in <br><a href = '/login'></b>" + \
"click here to log in</b></a>"
當(dāng)用戶瀏覽到“/login”login()視圖函數(shù)時(shí),因?yàn)樗峭ㄟ^GET方法調(diào)用的,所以將打開一個(gè)登錄表單。
表單發(fā)送回'/login',現(xiàn)在會(huì)話變量已設(shè)置。應(yīng)用程序重定向到'/'。此時(shí)會(huì)話變量'username'被找到。
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form action = "" method = "post">
<p><input type="text" name="username"/></p>
<p><input type="submit" value="Login"/></p>
</form>
'''
應(yīng)用程序還包含一個(gè)logout()視圖函數(shù),它會(huì)彈出'username'會(huì)話變量。因此,'/' URL再次顯示開始頁面。
@app.route('/logout')
def logout():
# remove the username from the session if it is there
session.pop('username', None)
return redirect(url_for('index'))
運(yùn)行應(yīng)用程序并訪問主頁。(確保設(shè)置應(yīng)用程序的secret_key)
from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
app.secret_key = 'any random string’
完整代碼如下所示
from flask import render_template
from flask import make_response
from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
app.secret_key = 'fkdjsafjdkfdlkjfadskjfadskljdsfklj'
@app.route('/')
def index():
if 'username' in session:
username = session['username']
return '登錄用戶名是:' + username + '<br>' + \
"<b><a href = '/logout'>點(diǎn)擊這里注銷</a></b>"
return "您暫未登錄, <br><a href = '/login'></b>" + \
"點(diǎn)擊這里登錄</b></a>"
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
return redirect(url_for('index'))
return '''
<form action = "" method = "post">
<p><input type="text" name="username"/></p>
<p><input type="submit" value ="登錄"/></p>
</form>
'''
@app.route('/logout')
def logout():
# remove the username from the session if it is there
session.pop('username', None)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug = True)
輸出將顯示如下。點(diǎn)擊“點(diǎn)擊此處登錄”鏈接。
鏈接將被定向到另一個(gè)屏幕。鍵入“admin”。
屏幕會(huì)顯示消息“ 登錄用戶名是:admin ”
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: