【问题标题】:Django/Daphne/Nginx Websockets not connectingDjango/Daphne/Nginx Websockets 未连接
【发布时间】:2022-06-29 21:36:48
【问题描述】:

我将 Django 与 Daphne 和 Nginx 一起使用来运行我的 websocket。在生产中几个月来一切都运行良好,但是就在几天前,我的 websocket 无法连接说“与 'wss://mydomain:8001/clips/509/' 的 WebSocket 连接失败:”。

我检查了服务器日志,这是在输出中

2022-04-07 16:57:22,970 ERROR    [Failure instance: Traceback: <class 'AttributeError'>: 'NoneType' object has no attribute 'replace'
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/autobahn/websocket/protocol.py:2839:processHandshake
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/txaio/tx.py:366:as_future
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/ws_protocol.py:72:onConnect
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: --- <exception caught here> ---
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/twisted/internet/defer.py:151:maybeDeferred
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/daphne/server.py:200:create_application
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/routing.py:54:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:35:__call__
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:53:valid_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:72:validate_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:73:<genexpr>
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /home/django/Odyssey/venv/lib/python3.8/site-packages/channels/security/websocket.py:97:match_allowed_origin
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:376:urlparse
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: /usr/lib/python3.8/urllib/parse.py:430:urlsplit
Apr 07 16:57:22 ubuntu-s-1vcpu-1gb-nyc1-01 python[1276]: ]

我不明白为什么会突然发生这种情况,是不是某个依赖项被更新或弃用了?

【问题讨论】:

  • 您是否能够在开发机器上复制相同的问题?

标签: python django websocket


【解决方案1】:

你好,你用redis吗?我有一个类似的问题!这可能会有所帮助。

这是我的错误


6 月 29 日 12:47:45 hbb python[234376]:文件“/var/www/hbb/virtualenv/lib/python3.8/site-packages/channels/security/websocket.py”,第 73 行,在验证原点

6 月 29 日 12:47:45 hbb python[234376]: return any(

6 月 29 日 12:47:45 hbb python[234376]:文件“/var/www/hbb/virtualenv/lib/python3.8/site-packages/channels/security/websocket.py”,第 74 行,在

6 月 29 日 12:47:45 hbb python[234376]: pattern == "*" or self.match_allowed_origin(parsed_origin, pattern)

6 月 29 日 12:47:45 hbb python[234376]:文件“/var/www/hbb/virtualenv/lib/python3.8/site-packages/channels/security/websocket.py”,第 98 行,在match_allowed_origin

6 月 29 日 12:47:45 hbb python[234376]: parsed_pa​​ttern = urlparse(pattern.lower())

6 月 29 日 12:47:45 hbb python[234376]:文件“/usr/lib/python3.8/urllib/parse.py”,第 376 行,在 urlparse 中

6 月 29 日 12:47:45 hbb python[234376]: splitresult = urlsplit(url, scheme, allow_fragments)

6 月 29 日 12:47:45 hbb python[234376]:文件“/usr/lib/python3.8/urllib/parse.py”,第 430 行,在 urlsplit 中

Jun 29 12:47:45 hbb python[234376]: AttributeError: 'NoneType' object has no attribute 'replace' ~


我修复了这个购买检查 redis 服务器状态的问题。

"sudo systemctl status redis-server"


6 月 29 日 12:55:00 hbb systemd[1]:启动高级键值存储...

6 月 29 日 12:55:00 hbb systemd[1]: redis-server.service: 无法打开 PID 文件

/run/redis/redis-server.pid (yet?) 启动后:不允许操作

6 月 29 日 12:55:00 hbb systemd[1]:启动高级键值存储。


如您所见,服务器正在运行,但有几个错误。

我通过编辑 redis.service 文件解决了这个问题

nano /etc/systemd/system/redis.service

[服务]

ExecStop=/bin/kill -s TERM $MAINPID

ExecStartPost=/bin/sh -c "echo $MAINPID > /var/run/redis/redis.pid"

然后输入

sudo systemctl daemon-reload

sudo systemctl 启用 redis-server

sudo systemctl restart redis.service

【讨论】:

    猜你喜欢
    • 2021-08-06
    • 2020-06-09
    • 2021-03-07
    • 2017-07-14
    • 2019-07-23
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多