我正在使用Django(4.2.6),Django Rest Framework(3.14.0),Channels(4.0.0)和Redis(4.2.0)编程一个项目,它作为移动应用程序的后端.到目前为止,我开发的Rest API连接和端点没有任何问题.我try 通过websockets测试连接,但得到以下错误:

Exception inside application: Error connecting to localhost:6379. Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379).
Traceback (most recent call last):
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/connection.py", line 274, in connect
    await self.retry.call_with_retry(
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/retry.py", line 59, in call_with_retry
    return await do()
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/connection.py", line 681, in _connect
    reader, writer = await asyncio.open_connection(
  File "/usr/lib/python3.10/asyncio/streams.py", line 48, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1084, in create_connection
    raise OSError('Multiple exceptions: {}'.format(
OSError: Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/django/contrib/staticfiles/handlers.py", line 101, in __call__
    return await self.application(scope, receive, send)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/routing.py", line 62, in __call__
    return await application(scope, receive, send)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/routing.py", line 116, in __call__
    return await application(
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/consumer.py", line 94, in app
    return await consumer(scope, receive, send)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/consumer.py", line 58, in __call__
    await await_many_dispatch(
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/utils.py", line 50, in await_many_dispatch
    await dispatch(result)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/consumer.py", line 73, in dispatch
    await handler(message)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/djangochannelsrestframework/consumers.py", line 84, in websocket_connect
    await super().websocket_connect(message)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels/generic/websocket.py", line 173, in websocket_connect
    await self.connect()
  File "/home/tecnicus/Documentos/Repositorios/MyProyect/MyApp/api/consumers.py", line 16, in connect
    await self.model_change.subscribe()
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/Selene_Backend_Copy_Test/lib/python3.10/site-packages/djangochannelsrestframework/observer/base_observer.py", line 144, in subscribe
    await consumer.add_group(group_name)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/djangochannelsrestframework/consumers.py", line 102, in add_group
    await self.channel_layer.group_add(name, self.channel_name)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/channels_redis/core.py", line 504, in group_add
    await connection.zadd(group_key, {channel: time.time()})
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/client.py", line 605, in execute_command
    conn = self.connection or await pool.get_connection(command_name, **options)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/connection.py", line 1076, in get_connection
    await self.ensure_connection(connection)
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/connection.py", line 1109, in ensure_connection
    await connection.connect()
  File "/home/tecnicus/Documentos/Repositorios/virtualenvs/virtualenv1/lib/python3.10/site-packages/redis/asyncio/connection.py", line 282, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error connecting to localhost:6379. Multiple exceptions: [Errno 111] Connect call failed ('::1', 6379, 0, 0), [Errno 111] Connect call failed ('127.0.0.1', 6379).
WebSocket DISCONNECT /ws/ [127.0.0.1:33284]


对于提到的3个框架,我是个新手,所以我尽可能地按照它们的文档,以及我找到的有关它的教程进行操作.

Settings.py

INSTALLED_APPS = [

    'daphne',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    'channels',
    'rest_framework',
    'rest_framework_simplejwt',
    'rest_framework_simplejwt.token_blacklist',
    'djangochannelsrestframework',
    'django_cleanup.apps.CleanupConfig',
    'drf_yasg',
    'django_filters',
    'rangefilter',
    
    # Aplicaciones
    'MyApp',

    
]

ASGI_APPLICATION = 'SeleneServer.asgi.application'


CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
    },
}

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.SessionAuthentication'),
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}

SIMPLE_JWT = {
    "ACCESS_TOKEN_LIFETIME": timedelta(hours=12),
    "REFRESH_TOKEN_LIFETIME": timedelta(days=7),
    'UPDATE_LAST_LOGIN': True
}

Asgi.py


import os
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from django.core.asgi import get_asgi_application
import agenda.api.router


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

django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter({
    'http': django_asgi_app,
    'websocket': AuthMiddlewareStack( 
        URLRouter(
            agenda.api.router.websocket_urlpatterns
        )
    )
})

Router.py


from django.urls import re_path
from MyApp.api.consumers import MyConsumer


websocket_urlpatterns = [
    re_path(r"^ws/", MyConsumer.as_asgi()),
]

Consumers.py


from djangochannelsrestframework import permissions
from djangochannelsrestframework.generics import GenericAsyncAPIConsumer
from djangochannelsrestframework.mixins import ListModelMixin
from djangochannelsrestframework.observer import model_observer

from MyApp.models import MyModel
from MyApp.api.serializers import MyModelSerializer

class MyConsumer(ListModelMixin, GenericAsyncAPIConsumer):

    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    permissions = (permissions.AllowAny,)

    async def connect(self, **kwargs):
        await self.model_change.subscribe()
        await super().connect()

    @model_observer(MyModel)
    async def model_change(self, message, observer=None, **kwargs):
        await self.send_json(message)

    @model_change.serializer
    def model_serialize(self, instance, action, **kwargs):
        return dict(data=MyModelSerializer(instance=instance).data, action=action.value)


我没有使用docker或任何类似的东西,因为我只希望项目在上传到Vercel服务器之前首先在我的计算机上本地工作.


我try 在没有Daphne的情况下运行项目,因为我听说对于最新版本的通道,它不是必需的依赖项,但结果是在测试WebSocket时,服务器抛出错误,无法找到请求所针对的地址:

Not Found: /ws/      
[05/Mar/2024 02:31:15] "GET /ws/ HTTP/1.1" 404 3003).

我还try 从asgi.py中删除AuthMiddleware Stack,以测试不需要用户登录的连接,但无论是否使用Daphne,结果都是相同的.

我不知道你是否推荐其他方法来判断WebSocket连接,因为我已经使用了浏览器WebSocket Client for Chrome,WebSocket-Client for Python,总是在ws://127.0.0.1:8000/ws/,因为前端,将在Reaction Native开发,还不会开发,但我必须测试WebSocket系统是否可以工作,即使它是非常基本的.

推荐答案

频道需要一个Redis后端才能运行.您需要安装并启动REDIS才能解决该错误

Python相关问答推荐

Python多处理:当我在一个巨大的pandas数据框架上启动许多进程时,程序就会陷入困境

rame中不兼容的d类型

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

2D空间中的反旋算法

如何在类和classy-fastapi -fastapi- followup中使用FastAPI创建路由

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

基于索引值的Pandas DataFrame条件填充

递归访问嵌套字典中的元素值

将JSON对象转换为Dataframe

计算分布的标准差

Geopandas未返回正确的缓冲区(单位:米)

Python Pandas—时间序列—时间戳缺失时间精确在00:00

跳过嵌套JSON中的级别并转换为Pandas Rame

如何使用使用来自其他列的值的公式更新一个rabrame列?

Numpyro AR(1)均值切换模型抽样不一致性

用fft计算指数复和代替求和来模拟衍射?

SpaCy:Regex模式在基于规则的匹配器中不起作用

Pandas:计数器的滚动和,复位

Scipy.linprog的可行性有问题吗?(A_ub@x0<;=b_ub).all()为True-但是-linprog(np.zeros_like(X0),A_ub=A_ub,b_ub=b_ub)不可行

如何在不不断遇到ChromeDriver版本错误的情况下使用Selify?