1.cookie
新建一个cook,py的蓝本文件
from flask import Blueprint,request,make_response
cookie = Blueprint('cookie',__name__,url_prefix='/cook')
# 查询
@cookie.route('/')
def get_cookie():
# return 'cookie'
return request.cookies.get('name','查无此人')
# 设置
@cookie.route('/set/')
def set_cookie():
resp = make_response('已设置')
resp.set_cookie('name','小花')
return resp
# 删除cookie
@cookie.route('/del/')
def del_cookie():
resp = make_response('删除cookie')
resp.delete_cookie('name')
return resp
2.cookie,session的启动文件
新建一个manage.py
from flask import Flask
from flask_script import Manager
from cook import cookie
from sess import sess
app = Flask(__name__)
manager = Manager(app)
app.register_blueprint(cookie)
@app.route('/')
def index():
return 'hello'
if __name__ == '__main__':
manager.run()
3.session文件
sess.py
from flask import Blueprint,session
sess = Blueprint('session',__name__,url_prefix='/sess')
@sess.route('/get/')
def get_session():
# return '获得session'
# 需要引入session,get,set,delete都是session自带的方法
return session.get('name','hello sess')
# 设置
@sess.route('/set/')
def set_session():
session['name']='翠花'
return 'session set ok'
# 删除
@sess.route('/del/')
def del_session():
# session.pop('name',None)
session.clear()
return 'session clear ok'
4.单独的session文件
新建一个manage2.py,单独运行
from flask import Flask,session
from flask_script import Manager
from redis import Redis
from flask_session import Session
app = Flask(__name__)
app.config['SECRET_KET'] = '123456'
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = Redis()
manager = Manager(app)
sess = Session(app)
@app.route('/')
def index():
return 'session测试'
@app.route('/get/')
def get_session():
return session.get('name','你是?')
@app.route('/set/')
def set_session():
session['name'] = '大花'
return 'session set ok'
@app.route('/del/')
def del_session():
session.pop('name',None)
return 'session clear'
if __name__ == '__main__':
manager.run()