【问题标题】:AutobahnPython w/Twisted: ssl.optionsForClientTLS on websocket.WampWebSocketClientFactoryAutobahnPython w/Twisted:websocket.WampWebSocketClientFactory 上的 ssl.optionsForClientTLS
【发布时间】:2015-08-09 03:26:25
【问题描述】:

我无法使用“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()

【问题讨论】:

    标签: twisted autobahn twisted.client


    【解决方案1】:

    没关系,想通了...希望这可以帮助其他面临同样问题的人。原来“connectWS”接受带有“optionsForClientTLS”的“contextFactory”。现在我们有了一个具有严格证书验证的自动重新连接 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      相关资源
      最近更新 更多