Django daphne asgi:Django只能处理ASGI/HTTP连接,而不能处理websocket [英] Django daphne asgi: Django can only handle ASGI/HTTP connections, not websocket

查看:882
本文介绍了Django daphne asgi:Django只能处理ASGI/HTTP连接,而不能处理websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很令人沮丧,我确实按照文档进行了所有设置,但是当我尝试独立运行daphne时,它总是抛出错误,当我使用 python manage.py运行服务器时,它确实可以正常工作.这非常令人沮丧,我似乎在其他任何地方都找不到类似的错误

This is really frustrating, I did set everything up according to the documentation, but daphne keeps throwing an error when I try to run it independently, it does work correctly when I use python manage.py run server. this is very frustrating and I can't seem to find similar error anywhere else

2020-01-25 09:57:17,627 INFO     Starting server at tcp:port=8000:interface=127.0.0.1
2020-01-25 09:57:17,628 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2020-01-25 09:57:17,628 INFO     Configuring endpoint tcp:port=8000:interface=127.0.0.1
2020-01-25 09:57:17,629 INFO     Listening on TCP address 127.0.0.1:8000
127.0.0.1:44516 - - [25/Jan/2020:09:57:27] "WSCONNECTING /ws/score/" - -
2020-01-25 09:57:28,637 ERROR    Exception inside application: Django can only handle ASGI/HTTP connections, not websocket.
  File "/home/sofdeath/.local/lib/python3.7/site-packages/daphne/cli.py", line 30, in asgi
    await self.app(scope, receive, send)
  File "/home/sofdeath/.local/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 146, in __call__
    % scope['type']
  Django can only handle ASGI/HTTP connections, not websocket.
127.0.0.1:44516 - - [25/Jan/2020:09:57:28] "WSDISCONNECT /ws/score/" - -
^C2020-01-25 09:57:39,126 INFO     Killed 0 pending application instances

这是我的asgi.py

here is my asgi.py

import os

from django.core.asgi import get_asgi_application

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

application = get_asgi_application()

我的routing.py:

my routing.py:

from django.urls import re_path
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator
import judge.routing

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            URLRouter(
                judge.routing.websocket_urlpatterns
            )
        )
    ),
})

并在我的settings.py中:

and in my settings.py:

INSTALLED_APPS = [
    ...
    'channels',
]
...
WSGI_APPLICATION = 'tabulator.wsgi.application'
ASGI_APPLICATION = "tabulator.routing.application"
...
CHANNEL_LAYERS = {
    "default": {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}

推荐答案

将您的 asgi.py 更改为此:

import os
import django
from channels.routing import get_default_application

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

django.setup()

application = get_default_application()

这篇关于Django daphne asgi:Django只能处理ASGI/HTTP连接,而不能处理websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆