Websocket连接高速公路和python中的twisted

2024-10-03 11:21:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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}$

不管我做了什么,我一做工厂就关门了


Tags: fromimportselffactorydeftwistedutf8websocket
1条回答
网友
1楼 · 发布于 2024-10-03 11:21:41

reactor.connectTCP接受IP地址(或主机名)作为其第一个参数。你给它传递了一个URI。它搞糊涂了,决定这一定是主机名,试图解决它,但失败了,并停止了客户端工厂。在

尝试传递real.okcoin.com而不是完整的URI。这可以解析为一个IP地址(我假设),并且连接尝试将能够继续进行。在

相关问题 更多 >