我无法使用'applicationRunner',因为它不支持对websockets进行自动重新连接(GitHub问题:#295 #389)。我已经恢复使用扭曲的'ReconnectingClientFactory'的弃用方法。我的问题是这种方法似乎不允许我添加'ssl.optionsForClientTLS'来严格验证服务器主机名和证书。有没有办法实现这个目标?
真的很感激任何反馈!
class MyClientFactory(websocket.WampWebSocketClientFactory, ReconnectingClientFactory):
maxDelay = config.maxretry
def clientConnectionFailed(self, connector, reason):
logging.debug("Connection Failed: %s", reason)
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason):
logging.debug("Connection Lost: %s", reason)
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def start():
log.startLogging(sys.stdout)
component_config = types.ComponentConfig()
session_factory = wamp.ApplicationSessionFactory(config=component_config)
session_factory.session = MyFrontendComponent
transport_factory = MyClientFactory(session_factory, 'wss://services:8443/ws', debug=True)
transport_factory.debug = True
transport_factory.debugCodePaths = True
websocket.connectWS(transport_factory)
Thread(target=reactor.run, args=(False,)).start()
答案 0 :(得分:0)
没关系,想通了......希望这可以帮助其他人面对同样的问题。事实证明'connectWS'接受'contextFactory'和'optionsForClientTLS'。现在我们有一个自动重新连接的WebSocket,它具有严格的证书验证:
class MyClientFactory(websocket.WampWebSocketClientFactory, ReconnectingClientFactory):
maxDelay = 30
def clientConnectionFailed(self, connector, reason):
logging.debug("Connection Failed: %s", reason)
self.retry(connector)
def clientConnectionLost(self, connector, reason):
logging.debug("Connection Lost: %s", reason)
self.retry(connector)
def start():
log.startLogging(sys.stdout)
component_config = types.ComponentConfig()
session_factory = wamp.ApplicationSessionFactory(config=component_config)
session_factory.session = MyFrontendComponent
transport_factory = MyClientFactory(session_factory, 'wss://services:8443/ws', debug=True)
transport_factory.debug = True
transport_factory.debugCodePaths = True
context_factory = None
if transport_factory.isSecure:
certData = getModule(__name__).filePath.sibling('server_cert.pem').getContent()
authority = ssl.Certificate.loadPEM(certData)
context_factory = ssl.optionsForClientTLS(u'services', authority)
websocket.connectWS(transport_factory, context_factory)
Thread(target=reactor.run, args=(False,)).start()