将 Autobahn websocket 添加到嵌套的 Twisted 资源

Add an Autobahn websocket to a nested Twisted resource

我有一个 Twisted 网络服务器,它同时服务于静态站点和 Autobahn 网络套接字。 websocket 作为子项添加到自定义站点,如下所示:

    self.factory = WebSocketServerFactory(address+":"+str(port), debug=False)
    self.factory.protocol = self.getWebSocketProtocol()

    resource = WebSocketResource(self.factory)

    staticfilepath = kwargs['staticfilepath'].encode('utf-8')
    websocketpath = kwargs['websocketpath'].encode('utf-8')
    root = CustomFile(staticfilepath)
    root.putChild(websocketpath, resource)

这没问题。问题是我现在需要将 websocket 添加到嵌套路径(例如,而不是 websocketpath="ws" 使用 websocketpath="sockets/ws")。我试过拆分 websocketpath 然后为每个级别创建一个资源,将 websocket 添加到最后但它似乎不起作用。

我追踪到 python Twisted 中的三个字符串问题。如果 url 输入尾部斜杠,则一切正常。但是,如果没有尾部斜杠,则会调用 Twisted 函数 addSlash,这会导致 Python 中出现未处理的异常 3. 我对 Twisted 代码进行了修复,它与两个 python 版本都兼容,似乎是工作正常。