Python中的Websocket消息处理程序

2024-04-19 04:00:01 发布

您现在位置: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中的各个值作为变量内存。在

例如:

香奈德=1528

价格=6613.2

我需要这个Python模块:https://pypi.org/project/websocket-client/

非常感谢你的帮助

代码如下:

^{pr2}$

Tags: 模块内存代码httpsorgprojectpypiclient
1条回答
网友
1楼 · 发布于 2024-04-19 04:00:01

这可能有帮助

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')

相关问题 更多 >