2016-12-18 100 views
3

我試圖用Python連接到BitStamp Websocket API在Pusher客戶端中接收事件

但是,我無法找到一個體面的教程或任何地方的過程的解釋。

我需要的是接收現場報價。

我嘗試使用this庫,但我沒有收到任何實時價格。我想我可能錯過了一些東西,因爲我是WebSockets的新手。

這裏是我的代碼:

import pusherclient 
import sys 

# Add a logging handler so we can see the raw communication data 
import logging 
root = logging.getLogger() 
root.setLevel(logging.INFO) 
ch = logging.StreamHandler(sys.stdout) 
root.addHandler(ch) 

global pusher 

# We can't subscribe until we've connected, so we use a callback handler 
# to subscribe when able 
def connect_handler(data): 
    channel = pusher.subscribe('live_trades') 
    channel.bind('trade', callback) 

appkey = "de504dc5763aeef9ff52" 
pusher = pusherclient.Pusher(appkey) 
pusher.connection.bind('pusher:connection_established', connect_handler) 
pusher.connect() 

print("finished") 

我看到,當我運行這段代碼是 - 成品

我怎麼能接受的價格實時更新?

+0

你找到獲取數據,然後通過StreamHandler中的更好的辦法? –

回答

2

的代碼的末尾添加一個while循環:

while True: 
    time.sleep(1)