【发布时间】:2016-02-19 11:11:15
【问题描述】:
我正在尝试订阅 Bitfinex.com websocket API 公共频道BTCUSD。
代码如下:
from websocket import create_connection
ws = create_connection("wss://api2.bitfinex.com:3000/ws")
ws.connect("wss://api2.bitfinex.com:3000/ws")
ws.send("LTCBTC")
while True:
result = ws.recv()
print ("Received '%s'" % result)
ws.close()
我相信ws.send("BTCUSD")是订阅公共频道的?我收到一条消息,我认为正在确认订阅({"event":"info","version":1},但之后我没有收到数据流。我错过了什么?
更新:这是最终有效的代码。
import json
from websocket import create_connection
ws = create_connection("wss://api2.bitfinex.com:3000/ws")
#ws.connect("wss://api2.bitfinex.com:3000/ws")
ws.send(json.dumps({
"event": "subscribe",
"channel": "book",
"pair": "BTCUSD",
"prec": "P0"
}))
while True:
result = ws.recv()
result = json.loads(result)
print ("Received '%s'" % result)
ws.close()
【问题讨论】:
-
如果您正在使用 API,请先阅读文档。 docs.bitfinex.com/#subscribe-to-channels
-
我已经通读了这些文件。据我所知,我缺少订阅频道的正确代码。然而,通过pypi.python.org/pypi/websocket-client 此处的所有文档,然后 send('LTCBTC') 是正确的订阅方式。我做错了什么?
标签: python api python-3.x websocket