-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/shicky/aiohttp_session in…
…to shicky-master
- Loading branch information
Showing
2 changed files
with
40 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import asyncio | ||
import time | ||
import base64 | ||
from cryptography import fernet | ||
from aiohttp import web | ||
from aiohttp_session import setup, get_session, session_middleware | ||
from aiohttp_session.cookie_storage import EncryptedCookieStorage | ||
|
||
@asyncio.coroutine | ||
def handler(request): | ||
session = yield from get_session(request) | ||
last_visit = session['last_visit'] if 'last_visit' in session else None | ||
session['last_visit'] = time.time() | ||
text = 'Last visited: {}'.format(last_visit) | ||
return web.Response(body=text.encode('utf-8')) | ||
|
||
app = web.Application() | ||
# secret_key must be 32 url-safe base64-encoded bytes | ||
fernet_key = fernet.Fernet.generate_key() | ||
secret_key = base64.urlsafe_b64decode(fernet_key) | ||
setup(app, EncryptedCookieStorage(secret_key)) | ||
app.router.add_route('GET', '/', handler) | ||
web.run_app(app) | ||
|