Skip to content

Instantly share code, notes, and snippets.

@alex-oleshkevich
Last active August 28, 2020 16:35
Show Gist options
  • Save alex-oleshkevich/80dab249a8fef9e47f1f82bed74d2c34 to your computer and use it in GitHub Desktop.
Save alex-oleshkevich/80dab249a8fef9e47f1f82bed74d2c34 to your computer and use it in GitHub Desktop.
[medium] django 3 websockets. middleware
from django.urls import resolve
from .connection import WebSocket
def websockets(app):
async def asgi(scope, receive, send):
if scope["type"] == "websocket":
match = resolve(scope["raw_path"])
await match.func(WebSocket(scope, receive, send), *match.args, **match.kwargs)
return
await app(scope, receive, send)
return asgi
@kornpow
Copy link

kornpow commented Aug 28, 2020

I am getting this issue here:
File "./mysite/asgi.py", line 13, in <module> from shop.middleware import websockets File "./shop/middleware.py", line 11 await app(scope, receive, send) ^ SyntaxError: 'await' outside async function

If I try and indent await app... and return asgi one level, I get: File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "./shop/middleware.py", line 11, in asgi await app(scope, receive, send) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 161, in __call__ response = await self.get_response_async(request) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 148, in get_response_async response = await self._middleware_chain(request) TypeError: object HttpResponse can't be used in 'await' expression

@alex-oleshkevich
Copy link
Author

Ooops, my bad. The def websocket has to be async def websocket

@kornpow
Copy link

kornpow commented Aug 28, 2020

That still doesnt work, because you are trying to call a coroutine in asgi.py

I've tried these things in asgi.py and still cannot get it to work:


from django.core.asgi import get_asgi_application
from shop.middleware import websockets
import asyncio

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_asgi_application()
# loop = asyncio.get_event_loop()
# application = asyncio.ensure_future(websockets(application))
application = await websockets(application)
# application = asyncio.run(websockets(application))
# application = websockets(application)

@kornpow
Copy link

kornpow commented Aug 28, 2020

Also in middleware.py the return asgi is on the same level as the function definition. It is kind of ambiguous to me what indentation needs to be fixed. Should return asgi be indented in addition to await app(scope, receive, send) or just the return?

@alex-oleshkevich
Copy link
Author

Try now

@kornpow
Copy link

kornpow commented Aug 28, 2020

ERROR: Exception in ASGI application Traceback (most recent call last): File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi result = await app(self.scope, self.receive, self.send) File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "./shop/middleware.py", line 21, in asgi await app(scope, receive, send) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 161, in __call__ response = await self.get_response_async(request) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 148, in get_response_async response = await self._middleware_chain(request) TypeError: object HttpResponse can't be used in 'await' expression Exception in ASGI application Traceback (most recent call last): File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi result = await app(self.scope, self.receive, self.send) File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "./shop/middleware.py", line 21, in asgi await app(scope, receive, send) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 161, in __call__ response = await self.get_response_async(request) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 148, in get_response_async response = await self._middleware_chain(request) TypeError: object HttpResponse can't be used in 'await' expression INFO: - "GET /favicon.ico HTTP/1.0" 500 Internal Server Error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment