我正在尝试使用Twested with autobahn连接到websocket服务器
from autobahn.twisted.websocket import WebSocketClientProtocol
class OkcClient(WebSocketClientProtocol):
def onOpen(self):
#self.sendMessage(u"Hello, world!".encode('utf8'))
self.sendMessage(u"{'event':'addChannel','channel':'ok_btcusd_future_ticker_this_week'}".encode('utf8'))
self.sendMessage(u"{'event':'addChannel','channel':'ok_future_btcusd_kline_this_week_5min'}".encode('utf8'))
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')))
import sys
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory
log.startLogging(sys.stdout)
factory = WebSocketClientFactory("wss://real.okcoin.com:10440/websocket/okcoinapi")
factory.protocol = OkcClient
reactor.connectTCP("wss://real.okcoin.com/websocket/okcoinapi", 10440, factory)
reactor.run()
但我从中得到的唯一信息是:
^{pr2}$不管我做了什么,我一做工厂就关门了
reactor.connectTCP
接受IP地址(或主机名)作为其第一个参数。你给它传递了一个URI。它搞糊涂了,决定这一定是主机名,试图解决它,但失败了,并停止了客户端工厂。在尝试传递
real.okcoin.com
而不是完整的URI。这可以解析为一个IP地址(我假设),并且连接尝试将能够继续进行。在相关问题 更多 >
编程相关推荐