from flask import Flask,session
import os
app = Flask(__name__)
# 从配置文件读取app.config['SECRET_KEY'] = os.urandom[24]
# app.config['SECRET_KEY'] = os.urandom(24)
#如果设置成随机的,则重新运行时无法获取username,解决方法就是设置一个固定的SECRET_KEY
app.config['SECRET_KEY'] = '123456'
#将session加密后存储到cookie
#只设置下面这句貌似不行
# SECRET_KEY = 'abc'
@app.route('/')
def hello_world():
session['username'] = 'zhiliao'
return 'Hello World'
@app.route('/get')
def get_session():
return session.get('username')
@app.route('/delete')
def delete_session():
print(session.get('username'))
session.pop('username')
print(session.get('username'))
return 'success'
@app.route('/clear')
def clear():
print(session.get('username'))
#删除session中的所有数据
session.clear()
print(session.get('username'))
return 'success'
if __name__ == '__main__':
app.run(debug= True)
Flask-session
最新推荐文章于 2024-09-06 14:32:16 发布