【发布时间】:2021-08-12 21:34:27
【问题描述】:
我在连接到用于加密交换 FTX 的 websocket api 时遇到问题。以下链接是我作为初学者不太清楚的文档。 https://docs.ftx.com/?python#websocket-api
下面是我用来尝试获取票证数据的代码。
import pandas as pd
import numpy as np
import json
import hmac
import time
import websocket
socket = 'wss://ftx.com/ws/'
api_key = xxxx
secret_key = xxxx
def on_open(ws):
print('connected')
ts = int(time.time() * 1000)
signa = hmac.new(secret_key.encode(), f'{ts}websocket_login'.encode(), 'sha256').hexdigest()
auth = {'op': 'login', 'args': {'key': api_key,
'sign': signa,
'time': ts}}
ws.send(json.dumps(auth))
data = {'op': 'subscribe', 'channel': 'ticker', 'market': 'BTC-PERP'}
ws.send(json.dumps(data))
def on_close(ws):
print('disconnected')
def on_message(ws,message):
print('got message')
json_msg = json.loads(message)
print(json_msg)
def on_error(ws,error):
print(error)
ws = websocket.WebSocketApp(socket,on_open=on_open,on_close=on_close,on_message=on_message,on_error=on_error)
ws.run_forever()
我只是无法连接到它。我收到以下消息:
[WinError 10060] 连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应
如果有人可以启发我了解它应该如何工作。 谢谢
【问题讨论】:
-
您正在添加一个 api 密钥和一个密钥作为变量,但是您的请求似乎没有遵循 docs.ftx.com/?python#authentication 的指导方针
-
不确定它是否使用与其余 API 和 websocket 相同的签名。查看示例rest api是:
signature_payload = f'{ts}{prepared.method}{prepared.path_url}'.encode() signature = hmac.new('YOUR_API_SECRET'.encode(), signature_payload, 'sha256').hexdigest()和websocket是hmac.new(self._api_secret.encode(),f'{ts}websocket_login'.encode(), 'sha256').hexdigest()
标签: python-3.x api websocket