from flask import Flask
from flask import render_template
from flask import request
from flask import make_response
from datetime import datetime, timedelta
 
 
@app.route('/set_cookie')
def set_cookie():
    # 设置 cookie
    response = make_response('Hello World')
    out_date = datetime.today() + timedelta(days=30)
    response.set_cookie('Name', 'Hyman', expires=out_date)
    return response
 
 
@app.route('/get_cookie')
def get_cookie():
    # 获取 cookie
    name = request.cookies.get('Name')
    print(name)
    return render_template('cookie.html')
 
 
@app.route('/del_cookie')
def del_cookie():
    # 清除 cookie1
    response = make_response('delete cookie')
    response.set_cookie('Name', '', expires=0)
    return response
 
 
@app.route('/del_cookie2')
def del_cookie2():
    # 清除 cookie2
    response = make_response('delete cookie2')
    response.delete_cookie('Name')
    return response
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>页面获取cookie</title>
</head>
<body>
    <h1>My name is {{request.cookies.get('Name')}}</h1>
</body>
</html>

转自:https://blog.csdn.net/guoqianqian5812/article/details/75305697

import os
from flask import Flask
from flask import session
from datetime import timedelta
 
app = Flask(__name__)
 
 
if __name__ == '__main__':
    app.run(debug=True)
 
 
app.config['SECRET_KEY'] = os.urandom(24)   # 设置为 24 位的字符,每次运行服务器都是不同的,所以服务器启动一次上次的 session 就清除。
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)  # 设置 session 的保存时间。
 
 
@app.route('/')
def index():
    session.permanent = True
    session['username'] = 'xxx'
    return 'Hello World!'
 
 
@app.route('/get/')
def get():
    return session.get('username')
 
 
@app.route('/delete/')
def delete():
    print(session.get('username'))
    session.pop('username')
    print(session.get('username'))
    return 'delete'
 
 
@app.route('/clear/')
def clear():
    print(session.get('username'))
    session.clear()
    print(session.get('username'))
    return 'clear'

转自:https://www.cnblogs.com/nimingdaoyou/p/9037655.html