为什么在启动Daphne for prod / IIS时会出现错误?

wztqucjr  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(278)

我在SSE和Channels上使用Django eventstream(我不能使用Memurai或Redis)。当我运行本地服务器时,一切都运行得很好。用户可以提交一个文件,该信息将通过它调用的外部第三方API运行,真实的信息将从/events/ URL API显示回浏览器。
当通过IIS进行配置时,除了/events/ URL不可访问(404)之外,一切都工作得很好,因此SSE连接不会启动,这会停止其轨道中的一切。
使用命令:python manage.py运行服务器IP:端口

Django version 3.2.7, using settings 'mysite.settings'
Starting ASGI/Channels version 3.0.4

WebApp仅为本地网络托管。
现在,使用Daphne进行prod时,我会收到各种错误:

daphne mysite.asgi:应用程序:

Traceback (most recent call last):
  File "C:\Python\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Python\Scripts\daphne.exe\__main__.py", line 7, in <module>
  File "C:\Python\lib\site-packages\daphne\cli.py", line 170, in entrypoint
    cls().run(sys.argv[1:])
  File "C:\Python\lib\site-packages\daphne\cli.py", line 232, in run
    application = import_by_path(args.application)
  File "C:\Python\lib\site-packages\daphne\utils.py", line 12, in import_by_path
    target = importlib.import_module(module_path)
  File "C:\Python\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\inetpub\wwwroot\mysite\.\mysite\asgi.py", line 15, in <module>
    from myapp1.routing import ws_urlpatterns
  File "C:\inetpub\wwwroot\mysite\.\myapp1\routing.py", line 2, in <module>
    from .consumers import WSConsumer
  File "C:\inetpub\wwwroot\mysite\.\myapp1\consumers.py", line 19, in <module>
    from . import views
  File "C:\inetpub\wwwroot\mysite\.\myapp1\views.py", line 10, in <module>
    from myapp1 import myapp1_alarm_delete
  File "C:\inetpub\wwwroot\mysite\.\myapp1\myapp1_alarm_delete.py", line 30, in <module>
    from myapp1.models import myapp1User
  File "C:\inetpub\wwwroot\mysite\.\myapp1\models.py", line 4, in <module>
    class myapp1User(models.Model):
  File "C:\Python\lib\site-packages\django\db\models\base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Python\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Python\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Python\lib\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
  File "C:\Python\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

所以我尝试使用daphne settings.configure()

C:\inetpub\wwwroot\mysite>daphne settings.configure()
Traceback (most recent call last):
  File "C:\Python\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Python\Scripts\daphne.exe\__main__.py", line 7, in <module>
  File "C:\Python\lib\site-packages\daphne\cli.py", line 170, in entrypoint
    cls().run(sys.argv[1:])
  File "C:\Python\lib\site-packages\daphne\cli.py", line 232, in run
    application = import_by_path(args.application)
  File "C:\Python\lib\site-packages\daphne\utils.py", line 11, in import_by_path
    module_path, object_path = path.split(":", 1)
ValueError: not enough values to unpack (expected 2, got 1)

阿斯吉.皮

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

from myApp1.routing import ws_urlpatterns
from django.conf import settings
from django.conf.urls import url
import django_eventstream

from mysite.settings import DEBUG

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

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter

application = ProtocolTypeRouter({
    'http': URLRouter([
        url(r'^events/', AuthMiddlewareStack(
            URLRouter(django_eventstream.routing.urlpatterns)
        ), { 'channels': ['test'] }),
        url(r'', get_asgi_application()),
    ]),
})

设置.py

import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = 'redacted'

DEBUG = True

ALLOWED_HOSTS = ['localhost', 'myserver']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp1',
    'myapp2',
    'myapp3',
    'channels',
    'django_eventstream',
]

LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'

SESSION_EXPIRE_AT_BROWSER_CLOSE = True

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_grip.GripMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR.joinpath('templates\myapp1')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'
ASGI_APPLICATION = 'mysite.asgi.application'

EVENTSTREAM_STORAGE_CLASS = 'django_eventstream.storage.DjangoModelStorage'

# Database

# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation

# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization

# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)

# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')# was prev removed, added for iis - this created mysite/static folder

# Default primary key field type

# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
7eumitmz

7eumitmz1#

在您的asgi文件中,将以下内容放在顶部

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
...
...
...

相关问题