使用安全WebSocket连接的Django通道-WSS://

问题描述 投票:8回答:2

[当我尝试使用sslserver如下所示运行Django应用程序时,

python manage.py runsslserver

错误:

Traceback:

Validating models...

System check identified no issues (0 silenced).
November 08, 2019 - 11:17:26
Django version 2.0.7, using settings 'dashboard_channels.settings'
Starting development server at https://127.0.0.1:8000/
Using SSL certificate: \lib\site-packages\sslserver\certs\development.crt
Using SSL key: \lib\site-packages\sslserver\certs\development.key
Quit the server with CTRL-BREAK.
[08/Nov/2019 11:18:33] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:34] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:35] "GET /static/js/jquery.js HTTP/1.1" 200 270575
Not Found: /ws/home
[08/Nov/2019 11:18:36] "GET /ws/home HTTP/1.1" 404 2134

浏览器控制台:

(index):31 WebSocket connection to 'wss://127.0.0.1:8000/ws/home' failed: Error during WebSocket handshake: Unexpected response code: 404
(index):41 error Event
(index):44 close CloseEvent

代码:

Javascript:

 var loc = window.location;
 var wsStart = 'ws://';
 if (loc.protocol == 'https:') {
     wsStart = 'wss://'
 }
 var endpoint = wsStart + loc.host + '/ws/home';

 var socket = new WebSocket(endpoint);

python manage.py runserver命令正常工作,对于http表示正常,但https无效。

如何解决此问题? (如何调试以解决此问题?)

还有其他方法可以在https门户上部署WebSocket吗?

仍然面对这个问题。谁能帮忙吗?

总之,这是出于测试目的,最后,我需要将其部署在Windows服务器计算机上的Apache2.4上。我已经为https设置但未为Web套接字设置的位置。

javascript python ssl websocket wss
2个回答
1
投票

我找到了答案,runserver命令正确地检测到asgi.py文件,并使用daphne在WebSockets上运行Django channel应用程序。 runsslserver不执行相同的工作,它正在运行wsgi.py文件而不是asgi.py文件。

[阅读了不同的方法之后,我知道我们可以使用普通的开发服务器(即使用HTTPS文件)处理wsgi.py请求,而使用wss(即使用Daphne文件)。

Daphne是一个官方设计的服务器,用于处理django通道(建在扭曲模块的顶部)。

因此,最后,我们需要运行两个服务器来分别处理asgi.pyhttps

wss

我们可以使用# In command prompt 1 python manage.py runsslserver 0.0.0.0:8000 # In command prompt 2 daphne -e ssl:8001:privateKey=cert\\private.pem:certKey=cert\\public.pem real_time_table.asgi:application 使用的相同SSL证书进行测试。

最后,在JavaScript中:

runsslserver

我希望,这可以节省别人的时间。


0
投票

感谢您在此处发布解决方案。但是,我有同样的问题,想知道,是否需要更改命令或其他任何地方以使我的工作正常。例如:“ daphne -e ssl:8001:privateKey = cert \ private.pem:certKey = cert \ public.pem real_time_table.asgi:application”可以直接使用此命令,而无需进行任何更改。抱歉,如果我很傻,但是我是Django,Daphne等的新手。感谢您的提前答复。顺便说一句,使用http(和ws)和python manage.py运行服务器,我的代码可以正常工作。但是当我使用https,was和runsslserver时不起作用。就像你一样...

© www.soinside.com 2019 - 2024. All rights reserved.