[英]Websocket message handler in Python
我已經成功訂閱了一個 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)
這可能有幫助
import json
import hmac
import hashlib
import time
from websocket import create_connection
key = ""
secret = ""
class Bitfinex(object):
def __init__(self, secret, key):
self.secret = secret
self.key = key
self.url = 'wss://api.bitfinex.com/ws/2'
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}
self.ws = create_connection(url)
def get_chanid(self, symbol):
self.ws.send(json.dumps(self.payload))
get_chanid = {
'event': "subscribe",
'channel': "ticker",
'symbol': "t" + symbol,
}
self.ws.send(json.dumps(get_chanid))
while True:
data = self.ws.recv()
print(data)
bit = Bitfinex(secret, key)
bit.get_chanid('BTCUSD')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.