flask的session基础认识

from flask import Flask,session
from datetime import timedelta
import os

app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)#随机24的字符串
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=14) #设置过期多少天

@app.route('/')
def hello_world():
    session['username'] = 'zhiliao' #操作session就像操作字典一样
    #permanent持久化的意思,可以设置过期时间,时间为一个月
    session.permanent = True
    return 'Hello World!'


@app.route('/get_session/')
def get_session():
    username = session.get('username')
    return username or '凉拌'


@app.route('/delete_session/')
def delete_session():
    session.pop('username')
    session.clear()#是全部删除,跟字典的用法一样
    return '删除成功'


if __name__ == '__main__':
    app.run()

 

posted @ 2018-10-05 15:45  python成长中  阅读(174)  评论(0编辑  收藏  举报