代码:
import websocket
ws = websocket.WebSocket()
ws.connect('wss://stream2.binance.com:9443/ws/!miniTicker@arr@3000ms')
record = ws.recv()
print(record)
我试图从Binance Websocket API中获取实时数据。在尝试使用此示例url获取数据时
wss://stream.binance.com:9443/ws/bnbbtc@depth
我得到这个错误,它说SSL验证失败。
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)
追踪: pastebin.com/RiHn025Z
我已经尝试过的:
因此,我在So How to create Python secure websocket client request?上找到了这个问题,并按照以下步骤编写了以下代码
ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE})
ws.connect("wss://stream2.binance.com:9443/ws/!miniTicker@arr@3000ms")
但随后出现了一个NameError:
NameError: name 'ssl' is not defined
我试着添加一个例外(这很荒谬,但仍然.)导致SyntaxError的发生。
其他Scopes
我尝试使用不同的websocket,它使用wss://,但是在第一个代码本身中工作得很好。
wss://ws.blockchain.info/inv
{"op":"ping"}
条件:
我在websockets.org上尝试了一个Echo测试,wss是完全功能的。
任何帮助都将不胜感激。还有其他模块专门用于二进制,但我想要原始数据,所以我使用这个api。
谢谢你读我的问题。
websocket的GitHub网址-client:https://github.com/websocket-client/websocket-client
发布于 2018-08-30 13:11:27
不确定websocket的新版本(0.51.0中的代码片段)发生了什么,但是旧的_http.py (链接)有一个if子句,新的一个只依赖于环境变量。不幸的是,我读过的任何文档中都没有列出它。花了很长时间才找到这个点,然后我找到了这个页面。似乎websocket客户端可以更好地处理这个问题。
def _ssl_socket(sock, user_sslopt, hostname):
sslopt = dict(cert_reqs=ssl.CERT_REQUIRED)
sslopt.update(user_sslopt)
certPath = os.environ.get('WEBSOCKET_CLIENT_CA_BUNDLE')
if certPath and os.path.isfile(certPath) \
and user_sslopt.get('ca_certs', None) is None \
and user_sslopt.get('ca_cert', None) is None:
sslopt['ca_certs'] = certPath
elif certPath and os.path.isdir(certPath) \
and user_sslopt.get('ca_cert_path', None) is None:
sslopt['ca_cert_path'] = certPath
https://stackoverflow.com/questions/49111583
复制相似问题