小编Ion*_*iuc的帖子

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

我正在尝试连接到wss://api.poloniex.com并订阅自动收报机.我在python中找不到任何有用的例子.我曾尝试使用autobahn/twisted和websocket-client 0.32.0.

这样做的目的是获取实时的自动收报机数据并将其存储在mysql数据库中.

到目前为止,我已尝试使用库文档中提供的示例.它们适用于localhost或测试服务器,但如果我改为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()
Run Code Online (Sandbox Code Playgroud)

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

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, …
Run Code Online (Sandbox Code Playgroud)

api websocket python-3.x

20
推荐指数
3
解决办法
2万
查看次数

标签 统计

api ×1

python-3.x ×1

websocket ×1