繁体   English   中英

使用Daphne服务Django和渠道

[英]Serving Django and Channels with Daphne

我在Google Compute Engine上有一个Django项目。 这是我的Django项目的结构。

example_channels
├── db.sqlite3
├── example
│   ├── admin.py
│   ├── apps.py
│   ├── consumers.py
│   ├── __init__.py
│   ├── migrations
│   │   
│   ├── models.py
│   ├── templates
│   │   └── example
│   │       ├── _base.html
│   │       └── user_list.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── example_channels
│   ├── asgi.py
│   ├── __init__.py
│   ├── routing.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

在学习教程之后,我做了一个asgi.py

import os
from channels.asgi import get_channel_layer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")

channel_layer = get_channel_layer()

我使用asgi_redis作为后端。 设置文件如下所示:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'asgi_redis.RedisChannelLayer',
        'CONFIG': {
            'hosts': [('localhost', 6379)],
        },
        'ROUTING': 'example_channels.routing.channel_routing',
    }
}

然后,我尝试启动服务器。 我运行python manage.py runworker &并获取:

~/websockets_prototype/example_channels$ 2017-07-19 16:04:19,204 - INFO - runworker - Usi
ng single-threaded worker.
2017-07-19 16:04:19,204 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisCha
nnelLayer)
2017-07-19 16:04:19,205 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconne
ct, websocket.receive

然后运行达芙妮:

~/websockets_prototype/example_channels$ 2017-07-19 16:05:28,619 INFO     Starting server
 at tcp:port=80:interface=0.0.0.0, channel layer example_channels.asgi:channel_layer.
2017-07-19 16:05:28,620 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2017-07-19 16:05:28,620 INFO     Using busy-loop synchronous mode on channel layer
2017-07-19 16:05:28,620 INFO     Listening on endpoint tcp:port=80:interface=0.0.0.0

然后,我开始向服务器发送请求,但出现“无法This site can't be reached错误。

尝试在端口8000中使用daphne,并在settings.py中查看您的ALLOWED_HOSTS = [“ *”]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM