使用Web的bitstamp orderbook

2024-10-17 08:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

下面用python编写的代码使用websocket实时获取比特戳交换的加密币(以太币以美元计)的价格。它在屏幕上打印下载的信息。在

import pusherclient
import logging

def connect_handler(data):
    trades_channel_ethusd = pusher.subscribe("live_trades_ethusd")
    trades_channel_ethusd.bind('trade', trade_callback_ethusd)

def trade_callback_ethusd(data):
    print(data)

pusher = pusherclient.Pusher("de504dc5763aeef9ff52")
pusher.connection.logger.setLevel(logging.WARNING) 

pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()

如果我想对orderbook执行同样的操作,我们应该将“live_trades_ethusd”更改为“diff_order_book_ethusd”。(^ a1)

但是,当我替换字符串时,它什么也不返回。是websocket失败吗?在

编辑:

正确的代码是:

^{pr2}$

Tags: 代码importdataloggingdefconnectchannelconnection
1条回答
网友
1楼 · 发布于 2024-10-17 08:22:34

简短回答

bind行改为以下内容

trades_channel_ethusd.bind('data', trade_callback_ethusd)

完整答案

订阅WebSocket时,请确保绑定到正确的事件名称。“previous”调用(即live_trades_ethusd)绑定到“trade”事件,其中diff_order_book_ethusd调用绑定到“data”事件

相关问题 更多 >