Cookie

使用Context的get_cookie(self) -> Dict[str, str]方法,可以获取已设置的Cookie键值对的dict。

使用Context的set_cookie(self, name, value, expires='', domain=None, secure=False, httponly=False, path=None)方法,可以设置Cookie。参数含义如下:

示例

from lessweb import Application, Context

def set_user(ctx: Context, name):
    ctx.response.set_cookie('username', name)
    return 'ok'

def get_user(ctx: Context):
    username = ctx.request.get_cookie('username')
    return {'user': username}

app = Application()
app.add_post_mapping('/set', dealer=set_user)
app.add_get_mapping('/get', dealer=get_user)
app.run()

请求1: curl -i -X POST -d "name=John" "http://localhost:8080/set"
返回:

HTTP/1.1 200 OK
Set-Cookie: username=John; Path=/
Content-Type: text/html; charset=utf-8
Content-Length: 2
Date: Fri, 08 Jun 2018 03:30:24 GMT
Server: Python/3.6 aiohttp/2.2.3

ok

请求2: curl --cookie "username=John" "http://localhost:8080/get"
返回: {"user": "John"}