上一章讲了flask的入门,这一章说一下flask中的cookie和session

设置cookie和获取cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#导包
from flask import Flask,make_response,request

#建立flask对象
app = Flask(__name__)

# 从配置文件来加载配置
app.config.from_pyfile('config.ini')

#使用flask路由器,指定网址和控制器,首页
@app.route('/')
def index():
return 'hello python'

#使用make_response库设置cookie
@app.route('/set_cookie')
def set_cookie():
resp = make_response('存储cookie')
#使用set_cookie来存储key-value形式的数据
resp.set_cookie('username','wzh')
return resp

#使用request库获取cookie
@app.route('/get_cookie')
def get_cookie():
resp = make_response('获取cookie')
#获取cookie需要使用request库
resp = request.cookies.get('username')
# resp = request.cookies['username']
return resp

if __name__ == '__main__':
app.run()

评论





载入天数...载入时分秒...

Blog content follows the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License

Use WZH as theme, total visits times