【nonebot2】QQ适配器websocket连接报错

1. 报错详细

05-27 14:54:21 [ERROR] nonebot | QQ | Error while setup websocket to wss://api.sgroup.qq.com/websocket. Trying to reconnect...
Traceback (most recent call last):
  File "/Users/xiayuan/Documents/Code/modernia-bot/bot.py", line 17, in <module>
    nonebot.run()
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/nonebot/__init__.py", line 337, in run
    get_driver().run(*args, **kwargs)
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/nonebot/drivers/fastapi.py", line 187, in run
    uvicorn.run(
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/uvicorn/main.py", line 580, in run
    server.run()
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/uvicorn/server.py", line 66, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/runners.py", line 194, in run
    return runner.run(main)
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
> File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/nonebot/adapters/qq/adapter.py", line 200, in _forward_ws
    async with self.websocket(request) as ws:
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py", line 214, in __aenter__
    return await anext(self.gen)
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/nonebot/internal/adapter/adapter.py", line 99, in websocket
    async with self.driver.websocket(setup) as ws:
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py", line 214, in __aenter__
    return await anext(self.gen)
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/nonebot/drivers/websockets.py", line 81, in websocket
    async with connection as ws:
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/websockets/legacy/client.py", line 633, in __aenter__
    return await self
  File "/Users/xiayuan/Documents/Code/modernia-bot/.venv/lib/python3.13/site-packages/websockets/legacy/client.py", line 652, in __await_impl__
    _transport, protocol = await self._create_connection()
  File "uvloop/loop.pyx", line 2088, in create_connection
    raise
  File "uvloop/loop.pyx", line 2083, in uvloop.loop.Loop.create_connection
    await ssl_waiter
  File "uvloop/sslproto.pyx", line 517, in uvloop.loop.SSLProtocol._on_handshake_complete
    raise handshake_exc
  File "uvloop/sslproto.pyx", line 499, in uvloop.loop.SSLProtocol._do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line 951, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1020)

问题分析(来自GPT):

这是一个典型的 SSL 证书验证失败 错误,原因是 NoneBot 的 QQ 适配器尝试连接 wss://api.sgroup.qq.com/websocket 时,发现返回的证书链中包含了自签名证书(self-signed certificate),而你的 Python 解释器出于安全考虑默认拒绝了它。

这通常在以下几种情况下出现:

系统或 Python 没有正确的 CA 根证书列表(在 macOS 上尤其常见)。

你本地或中间代理劫持了 SSL 流量并使用了自签名证书(比如公司内网、某些防火墙、某些 VPN)。

QQ 的服务器有问题,但可能性较小。

解决办法

使用 Python 安装证书工具(macOS 推荐)
你是在 macOS 下运行的 Python 3.13,可以尝试运行:

/Applications/Python\ 3.13/Install\ Certificates.command

这个命令会安装或修复 Python 使用的系统根证书,解决大部分 macOS 上的 CERTIFICATE_VERIFY_FAILED 问题。

其他关键词
nonebot2连接报错
QQ机器人websocket连接报错

posted @ 2025-06-14 11:40  夏源ovo  阅读(64)  评论(0)    收藏  举报