【发布时间】:2019-03-04 12:39:09
【问题描述】:
我已成功订阅 websocket 并正在接收数据。喜欢:
Received '[0, 'hb']'
Received '[1528, 'hb']'
Received '[1528, [6613.2, 21.29175815, 6613.3, 37.02985217, 81.6, 0.0125, 6611.6, 33023.06141807, 6826.41966538, 6491]]'
Received '[1528, 'hb']'
Received '[0, 'hb']'
现在我希望 python 中的单个值作为变量内存。
例如:
ChanID = 1528
价格 = 6613.2
我需要这个 Python 模块:https://pypi.org/project/websocket-client/
非常感谢您的帮助
代码如下:
import json
import hmac, hashlib
from time import time, sleep
from websocket import create_connection
url = 'wss://api.bitfinex.com/ws/2'
key = ""
secret = ""
ws = create_connection(url)
class Bitfinex():
def __init__(self, secret, key):
self.secret = secret
self.key = key
self.url = url
self.nonce = str(int(time.time() * 1000000))
self.signature = hmac.new(str.encode(self.secret), bytes('AUTH' + self.nonce, 'utf8'),
hashlib.sha384).hexdigest()
self.payload = {'apiKey': self.key,
'event': 'auth',
'authPayload': 'AUTH' + self.nonce,
'authNonce': self.nonce,
'authSig': self.signature}
ws.send(json.dumps(self.payload))
def get_chanid(self, symbol):
get_chanid = {
'event': "subscribe",
'channel': "ticker",
'symbol': "t" + symbol,
}
ws.send(json.dumps(get_chanid))
Bitfinex.__init__(Bitfinex, secret, key)
sleep(1)
Bitfinex.get_chanid(Bitfinex, 'BTCUSD')
sleep(1)
【问题讨论】:
-
向我们展示您的代码以及到目前为止您做了什么。您以哪种格式获取数据?
-
Json 格式
-
我建议学习类在 python 中的工作方式,您对
Bitfinex.__init__的使用可能没有达到您的预期