diff options
Diffstat (limited to 'testing/web-platform/tests/tools/third_party/websockets/example/django/authentication.py')
-rw-r--r-- | testing/web-platform/tests/tools/third_party/websockets/example/django/authentication.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/django/authentication.py b/testing/web-platform/tests/tools/third_party/websockets/example/django/authentication.py new file mode 100644 index 0000000000..f6dad0f55e --- /dev/null +++ b/testing/web-platform/tests/tools/third_party/websockets/example/django/authentication.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import asyncio + +import django +import websockets + +django.setup() + +from sesame.utils import get_user +from websockets.frames import CloseCode + + +async def handler(websocket): + sesame = await websocket.recv() + user = await asyncio.to_thread(get_user, sesame) + if user is None: + await websocket.close(CloseCode.INTERNAL_ERROR, "authentication failed") + return + + await websocket.send(f"Hello {user}!") + + +async def main(): + async with websockets.serve(handler, "localhost", 8888): + await asyncio.Future() # run forever + + +if __name__ == "__main__": + asyncio.run(main()) |