gpt4 book ai didi

api - 如何使用 python 库连接到 poloniex.com websocket api

转载 作者:行者123 更新时间:2023-12-02 05:54:50 28 4
gpt4 key购买 nike

我正在尝试连接到 wss://api.poloniex.com 并订阅股票代码。我在 python 中找不到任何工作示例。我尝试过使用 autobahn/twisted 和 websocket-client 0.32.0。

这样做的目的是获取实时行情数据并将其存储在 mysql 数据库中。

到目前为止,我已尝试使用库文档中提供的示例。它们适用于本地主机或测试服务器,但如果我更改为 wss://api.poloniex.com,我会收到一堆错误。

这是我使用 websocket-client 0.32.0 的尝试:

from websocket import create_connection
ws = create_connection("wss://api.poloniex.com")
ws.send("ticker")
result = ws.recv()
print "Received '%s'" % result
ws.close()

这是使用高速公路/扭曲:

from autobahn.twisted.websocket import WebSocketClientProtocol
from autobahn.twisted.websocket import WebSocketClientFactory


class MyClientProtocol(WebSocketClientProtocol):

def onConnect(self, response):
print("Server connected: {0}".format(response.peer))

def onOpen(self):
print("WebSocket connection open.")

def hello():
self.sendMessage(u"ticker".encode('utf8'))
self.sendMessage(b"\x00\x01\x03\x04", isBinary=True)
self.factory.reactor.callLater(1, hello)

# start sending messages every second ..
hello()

def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))

def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))


if __name__ == '__main__':

import sys

from twisted.python import log
from twisted.internet import reactor

log.startLogging(sys.stdout)

factory = WebSocketClientFactory("wss://api.poloniex.com", debug=False)
factory.protocol = MyClientProtocol

reactor.connectTCP("wss://api.poloniex.com", 9000, factory)
reactor.run()

如果有一个完整但简单的示例来展示如何使用任何 python 库连接和订阅 websocket 推送 api,我们将不胜感激。

最佳答案

这使用了未记录的 websocket 端点,因为 Poloniex 已经取消了对原始 WAMP 套接字端点的支持。

import websocket
import thread
import time
import json

def on_message(ws, message):
print(message)

def on_error(ws, error):
print(error)

def on_close(ws):
print("### closed ###")

def on_open(ws):
print("ONOPEN")
def run(*args):
ws.send(json.dumps({'command':'subscribe','channel':1001}))
ws.send(json.dumps({'command':'subscribe','channel':1002}))
ws.send(json.dumps({'command':'subscribe','channel':1003}))
ws.send(json.dumps({'command':'subscribe','channel':'BTC_XMR'}))
while True:
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())


if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://api2.poloniex.com/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()

channel 是:

1001 = trollbox (you will get nothing but a heartbeat)
1002 = ticker
1003 = base coin 24h volume stats
1010 = heartbeat
'MARKET_PAIR' = market order books

关于api - 如何使用 python 库连接到 poloniex.com websocket api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154121/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com