我正在尝试连接wss://api.poloniex.com并订阅代码。我在python中找不到任何有用的例子。我曾尝试使用autobahn/扭曲和websocket-客户端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()
这是使用高速公路/扭曲:
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。
发布于 2016-07-07 20:24:41
目前,Twisted没有正确使用Windows信任存储。因此,TLS证书的验证将失败。若要解决此问题,直到Twisted或Autobahn包含了解决方法为止,您可以:
certifi
模块(pip install certifi
)export SSL_CERT_FILE="$(python -m certifi)"
https://stackoverflow.com/questions/32154121
复制相似问题