在Flask中,可以使用request.cookies
和make_response
来实现Cookie管理。
from flask import request
@app.route('/')
def index():
cookie_name = request.cookies.get('name')
return f'Hello, {cookie_name}!'
from flask import make_response
@app.route('/set-cookie')
def set_cookie():
resp = make_response('Cookie has been set!')
resp.set_cookie('name', 'Alice')
return resp
from flask import make_response
@app.route('/delete-cookie')
def delete_cookie():
resp = make_response('Cookie has been deleted!')
resp.set_cookie('name', '', expires=0)
return resp
通过以上方法,可以实现对Cookie的简单管理和操作。