Windows Server 2019虚拟机Bybit WebSocket连接SSL证书验证失败求助
Bybit WebSocket连接SSL证书验证失败问题解决
问题场景
使用以下最简代码连接Bybit WebSocket时,本地设备运行正常,但Windows Server 2019虚拟机中出现连接失败:
from pybit.unified_trading import WebSocket from time import sleep ws = WebSocket( testnet=True, channel_type="linear", ) def handle_message(message): print(message) ws.orderbook_stream( depth=50, symbol="BTCUSDT", callback=handle_message ) while True: sleep(1)
报错信息
websocket._exceptions.WebSocketTimeoutException: WebSocket Unified V5 (Auth) (wss://stream.bybit.com/v5/private) connection failed. Too many connection attempts. pybit will no longer try to reconnect.
深层错误
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate.
已尝试升级certifi、pycryptodome、cryptography、requests、pybit等库,安装OpenSSL、IIS,但问题未解决。
问题根源
虚拟机的系统根证书存储缺少Bybit服务器证书对应的根颁发机构(CA)证书,Bybit通常使用Let's Encrypt的SSL证书,因此大概率是缺少Let's Encrypt的根CA证书(如ISRG Root X1/X2)。
解决方法
1. 手动安装根CA证书(推荐)
- 打开浏览器访问
https://stream.bybit.com,点击地址栏的锁图标,查看证书详情 - 在证书链中找到根证书(例如Let's Encrypt的ISRG Root X1),导出为Base64编码的CER格式文件
- 按Win+R输入
certmgr.msc打开证书管理器,展开「受信任的根证书颁发机构」→「证书」 - 右键选择「所有任务」→「导入」,选择导出的CER文件,按向导完成导入,确保证书存入「受信任的根证书颁发机构」存储
2. 让Python使用系统证书存储
修改代码,配置WebSocket使用系统默认的SSL上下文,替代certifi的证书:
from pybit.unified_trading import WebSocket from time import sleep import ssl ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH) ssl_context.load_default_certs() ws = WebSocket( testnet=True, channel_type="linear", ssl_options={"ssl_context": ssl_context} ) def handle_message(message): print(message) ws.orderbook_stream( depth=50, symbol="BTCUSDT", callback=handle_message ) while True: sleep(1)
3. 临时跳过证书验证(仅测试用,不推荐)
若仅用于临时测试,可关闭证书验证(存在安全风险):
from pybit.unified_trading import WebSocket from time import sleep ws = WebSocket( testnet=True, channel_type="linear", ssl_options={"verify": False} ) def handle_message(message): print(message) ws.orderbook_stream( depth=50, symbol="BTCUSDT", callback=handle_message ) while True: sleep(1)
内容的提问来源于stack exchange,提问作者Александр Вы ёбнутые
相关产品推荐
相关产品推荐

