与Daphne一起为Django和频道服务

时间:2017-07-19 16:06:50

标签: python django django-channels 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错误。

1 个答案:

答案 0 :(得分:0)

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