Flask中使用cookie和session

Flask中使用cookie和session

设置cookie


from flask import Flask,Response
app = Flask(__name__)

@app.route('/index')
def index():
    response = Response("设置cookie")
    response.set_cookie('username','sfencs')
    return response

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

获取cookie

username= request.cookies.get('username')

删除cookie


response.delete_cookie('username')

设置session

使用session需要设置SECRET_KEY


from flask import Flask,Response,session
app = Flask(__name__)

app.config['SECRET_KEY'] = ‘asdadasd’

@app.route('/index')
def index():
    session['username'] = 'sfencs'
    return 'session'
if __name__ == '__main__':
    app.run()

获取session


username = session.get('username')

删除session


 session.pop('username')  
 session.clear()          #删除所有session

    原文作者:sfencs
    原文地址: https://www.cnblogs.com/sfencs-hcy/p/10787613.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞