Python上带有Autobahn的WAMP websocket服务器连接错误

2024-05-15 17:21:40 发布

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

我正在尝试使用Python连接到WAMP websocket服务器并订阅接收消息,但我无法实现。我在JS上使用Autobahn成功连接,代码如下:

< script src = "autobahn.js" > < /script> <script >
  var conn = new ab.Session('ws://examplehost.com:8443/ws',
    function() {
      conn.subscribe('channel', function(topic, data) {
        console.log(data);
        alert('New data arrived: "' + topic + '" : ' + data.title);});},
    function() {
      console.warn('WebSocket connection closed');
    }, {'skipSubprotocolCheck': true}); 
  </script>

但在Python上使用相同的库并使用以下代码会导致404错误:

from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner

class Component(ApplicationSession):
    async def onJoin(self, details):
        def on_event(i):
            print("New data arrived: {}".format(i))

        await self.subscribe(on_event, 'channel')

if __name__ == '__main__':
    url = "ws://examplehost.com:8443/ws"
    runner = ApplicationRunner(url)
    runner.run(Component)

这就是我得到的错误:

failing WebSocket opening handshake ('WebSocket connection upgrade failed (404 - NotFound)')
dropping connection to peer tcp4:123.123.123.123:8443 with abort=True: WebSocket connection upgrade failed (404 - NotFound)

有没有办法解决这个问题?在搜索了很多之后,在服务器上使用路径/ws可能会导致一些问题,但我不确定。我也尝试过许多其他Python模块,但运气不好


Tags: 代码服务器comdatatopicwschannelscript