1

这是我的 asgi.py:

"""
ASGI config for VirtualLab_Server project.

It exposes the ASGI callable as a module-level variable named ``application``.


For more information on this file, see
https://docs.djangoproject.com/en/dev/howto/deployment/asgi/
""" 

import os
import django
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'VirtualLab_Server.settings')
django.setup()
application = get_asgi_application()

错误信息:

WebSocket HANDSHAKING
/tunnelws/ticket/0fa9dd05-0d91-4baa-923e-05f6af6f55a0/
[127.0.0.1:56637]
        Exception inside application: You cannot call this from an async context - use a thread or sync_to_async.
        Traceback (most recent call last):
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\fields\related_descriptors.py",
line 173, in __get__
            rel_obj = self.field.get_cached_value(instance)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\fields\mixins.py",
line 15, in get_cached_value
            return instance._state.fields_cache[cache_name]
        KeyError: 'user'
        
        During handling of the above exception, another exception occurred:
        
        Traceback (most recent call last):
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\staticfiles.py",
line 44, in __call__
            return await self.application(scope, receive, send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\routing.py",
line 71, in __call__
            return await application(scope, receive, send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\sessions.py",
line 47, in __call__
            return await self.inner(dict(scope, cookies=cookies), receive, send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\sessions.py",
line 254, in __call__
            return await self.inner(wrapper.scope, receive, wrapper.send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\auth.py",
line 181, in __call__
            return await super().__call__(scope, receive, send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\middleware.py",
line 26, in __call__
            return await self.inner(scope, receive, send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\routing.py",
line 150, in __call__
            return await application(
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\consumer.py",
line 94, in app
            return await consumer(scope, receive, send)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\consumer.py",
line 62, in __call__
            await await_many_dispatch([receive], self.dispatch)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\utils.py",
line 51, in await_many_dispatch
            await dispatch(result)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\consumer.py",
line 73, in dispatch
            await handler(message)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\channels\generic\websocket.py",
line 175, in websocket_connect
            await self.connect()
          File "E:\VirtualLab\VirtualLab\VirtualLab_Server\VirtualLab_Server\guacdproxy\consumer.py",
line 42, in connect
            if ticket.user != self.scope['user']:
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\fields\related_descriptors.py",
line 187, in __get__
            rel_obj = self.get_object(instance)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\fields\related_descriptors.py",
line 154, in get_object
            return qs.get(self.field.get_reverse_related_filter(instance))
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\query.py",
line 431, in get
            num = len(clone)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\query.py",
line 262, in __len__
            self._fetch_all()
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\query.py",
line 1324, in _fetch_all
            self._result_cache = list(self._iterable_class(self))
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\query.py",
line 51, in __iter__
            results = compiler.execute_sql(chunked_fetch=self.chunked_fetch,
chunk_size=self.chunk_size)
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\db\models\sql\compiler.py",
line 1167, in execute_sql
            cursor = self.connection.cursor()
          File "C:\Users\MarianPC\AppData\Local\Programs\Python\Python39\lib\site-packages\django-3.2b1-py3.9.egg\django\utils\asyncio.py",
line 24, in inner
            raise SynchronousOnlyOperation(message)
        django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
        
        this is my routing.py
        from channels.auth import AuthMiddlewareStack
        from channels.routing import ProtocolTypeRouter, URLRouter
        from django.urls import path
        from .guacdproxy import GuacamoleConsumer
        
        application = ProtocolTypeRouter({
            'websocket': AuthMiddlewareStack(
                URLRouter([
                    # path('tunnelws/ticket/<uuid:ticket>/', GuacamoleConsumer),
                    path('tunnelws/ticket/<uuid:ticket>/', GuacamoleConsumer.as_asgi()),
                ])
            ),
        })
        
4

0 回答 0