欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flask中使用cookie和session

程序员文章站 2022-04-28 14:35:34
Flask中使用cookie和session 设置cookie from flask import Flask,Response app = Flask(__name__) @app.route('/index') def index(): response = Response("设置cookie ......

flask中使用cookie和session

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()
username= request.cookies.get('username')
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