[英]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.