Flask框架的学习——12—(cookie和session、Flask上下文、常用的钩子函数、Restful API规范、Flask-Restful插件、参数解析、 输出字段、)

Kara ·
更新时间:2024-09-21
· 992 次阅读

1、cookie和session cookie:在网站中,http请求是无状态的。也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户。cookie的出现就是为了解决这个问题,第一次登录后服务器返回一些数据(cookie)给浏览器,然后浏览器保存在本地,当该用户发送第二次请求的时候,就会自动的把上次请求存储的cookie数据自动的携带给服务器,服务器通过浏览器携带的数据就能判断当前用户是哪个了。cookie存储的数据量有限,不同的浏览器有不同的存储大小,但一般不超过4KB。因此使用cookie只能存储一些小量的数据。 @app.route('/') def index(): # return '123' res = Response("首页") res.set_cookie('abc', '123', max_age=3) # key=abc, value=123, 3s后过期 ''' set_cookie()源码 def set_cookie( self, key, value="", max_age=None, # 失效时间 expires=None, # datetime形式的失效时间 path="/", # 在该路径下才能设置cookie domain=None, # 域名限制 secure=False, # 仅可通过HTTPS使用 httponly=False, samesite=None, ): :param key: the key (name) of the cookie to be set. :param value: the value of the cookie. :param max_age: should be a number of seconds, or `None` (default) if the cookie should last only as long as the client's browser session. :param expires: should be a `datetime` object or UNIX timestamp. :param path: limits the cookie to a given path, per default it will span the whole domain. :param domain: if you want to set a cross-domain cookie. For example, ``domain=".example.com"`` will set a cookie that is readable by the domain ``www.example.com``, ``foo.example.com`` etc. Otherwise, a cookie will only be readable by the domain that set it. :param secure: If `True`, the cookie will only be available via HTTPS ''' return res

原创文章 81获赞 13访问量 9024 关注 私信 展开阅读全文
作者:越过山丘,佳人等候



api规范 输出 学习 字段 参数 函数 restful 钩子函数 flask api session cookie

需要 登录 后方可回复, 如果你还没有账号请 注册新账号