flask中的cookie

#_*_ encoding: utf-8 _*_   @author: ty  hery   2019/12/20

from  flask import  Flask, request, abort, Response, make_response, jsonify
# from werkzeug.routing import BaseConverter
import json
app = Flask(__name__)

@app.route('/set_cookie',methods=['GET','POST'])
def set_cookie():
    # request中包含了前端发送过来的所有请求
    # 通过request.form可以直接提取请求体中的表单格式的数据,是一个类字典的对象
    resp = make_response('successs')
    # 设置cookie, 默认有效期是临时cookie,浏览器关闭就失效
    resp.set_cookie('Itcase',"python")
    resp.set_cookie('Itcase01',"python01")
    # max_age 设置有效期,单位秒
    resp.set_cookie('Itcase2','Python2',max_age=3600)
    resp.set_cookie('Itcase3','Python3',max_age=885)
    resp.headers['Set-Cookie'] = 'Itcase4=Python4; Expires=Sat, 21-Dec-2019 12:43:01 GMT; Max-Age=185'
    return resp

@app.route('/get_cookie')
def get_cookie():
    c = request.cookies.get('Itcase4')
    # c4 = request.cookies.get('Itcase4')
    return c

@app.route('/delete_cookie')
def delete_cookie():
    '''删除cookie'''
    resp = make_response('del success')
    resp.delete_cookie('Itcase')
    return resp

if __name__ == '__main__':
    print('--哈哈01--',app.url_map,'--哈哈01--')
    app.run(debug=True)
posted @ 2021-04-12 22:17  ty1539  阅读(52)  评论(0编辑  收藏  举报