Debian Codegraph @

third_party/python/aiohttp/examples/web_cookies.py

raw
1 #!/usr/bin/env python3
2 """Example for aiohttp.web basic server with cookies."""
3
4 from pprint import pformat
5 from typing import NoReturn
6
7 from aiohttp import web
8
9 tmpl = """\
10 <html>
11 <body>
12 <a href="/login">Login</a><br/>
13 <a href="/logout">Logout</a><br/>
14 <pre>{}</pre>
15 </body>
16 </html>"""
17
18
19 async def root(request):
20 resp = web.Response(content_type="text/html")
21 resp.text = tmpl.format(pformat(request.cookies))
22 return resp
23
24
25 async def login(request: web.Request) -> NoReturn:
26 exc = web.HTTPFound(location="/")
27 exc.set_cookie("AUTH", "secret")
28 raise exc
29
30
31 async def logout(request: web.Request) -> NoReturn:
32 exc = web.HTTPFound(location="/")
33 exc.del_cookie("AUTH")
34 raise exc
35
36
37 def init():
38 app = web.Application()
39 app.router.add_get("/", root)
40 app.router.add_get("/login", login)
41 app.router.add_get("/logout", logout)
42 return app
43
44
45 web.run_app(init())