高性能异步web框架Sanic文档【九】

来源:互联网 发布:企业培训网络课程设计 编辑:程序博客网 时间:2024/06/08 16:27
个人翻译自https://github.com/channelcat/sanic,在https://github.com/donghouhe/sanic可看到,现在再贴于CSDN上。MIT LICENSE

Cookies

请求

请求cookies可以通过request.cookie字典来取得

举例

from sanic import Sanicfrom sanic.response import text@app.route("/cookie")async def test(request):    test_cookie = request.cookies.get('test')    return text("Test cookie set to: {}".format(test_cookie))

Response

响应cookies能被像字典一样设置并且以下的参数是可用的:

响应cookies能被巷子店一样设置并且以下的参数时刻用的:

  • expires - datetime - 客户端浏览器cookie过期时间
  • path - string - 设置cookie 支持的url
  • comment - string - Cookie 注释 (元数据)
  • domain - string - 指定有效的域。 一个显式的指定域必须以点开始.
  • max-age - number - cookie应该存活的秒数
  • secure - boolean - 指定cookie是否只能通过HTTPS发送
  • httponly - boolean - 指定是否cookie不能被js读取

举例

from sanic import Sanicfrom sanic.response import text@app.route("/cookie")async def test(request):    response = text("There's a cookie up in this response")    response.cookies['test'] = 'It worked!'    response.cookies['test']['domain'] = '.gotta-go-fast.com'    response.cookies['test']['httponly'] = True    return response

0 0
原创粉丝点击