连接到横杆io要么挂断,要么断开

2024-05-14 10:56:22 发布

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

我试图用Python和autobahn连接到远程主机上的crossbar[Twisted]

我使用的是PubSub修改后的示例代码:

from __future__ import print_function
from os import environ
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner

class Component(ApplicationSession):
    def __init__(self, config=None):
       ApplicationSession.__init__(self, config)
        print("component created")

   def onConnect(self):
        print("transport connected")
        self.join(self.config.realm)

    def onChallenge(self, challenge):
        print("authentication challenge received")

    @inlineCallbacks
    def onJoin(self, details=None):
        print("session attached")
        self.received = 0
        for x in range(1, 501):
            sub = yield self.subscribe(self.on_event, u'com.myapp.topic{}'.format(x))
            if x % 100 == 0:
                print("Subscribed to {} topics".format(x))

    def on_event(self, i=None):
        print("Got event: {}".format(i))
        self.received += 1
        self.config.extra for configuration, etc. (see [A])
        if self.received > self.config.extra['max_events']:
            print("Received enough events; disconnecting.")
            self.leave()

    def onDisconnect(self):
        print("disconnected")
        if reactor.running:
            reactor.stop()


if __name__ == '__main__':
    runner = ApplicationRunner(
        url=u"ws://localhost:8080/ws",
        realm=u"realm1",
        extra=dict(
            max_events=5000,  # [A] pass in additional configuration
        ),
    )
    print(runner.log)
    runner.run(Component)

我在本地主机上运行crossbar的一个实例进行测试,当我访问它时,一切都正常。在

^{pr2}$

但如果我尝试连接到其他主机,会发生两件事: 如果是安全端口:

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected

(会话从不附加,程序挂起)

如果端口不安全:

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected
2016-04-01T17:26:19+0000 disconnected
2016-04-01T17:26:19+0000 Main loop terminated.

(在连接会话之前,连接会自动将我踢出

我要连接的主机有8080作为安全端口,8081用于不安全端口。所以我改变的是:

url=u'ws://{hostname}:8080/ws', (or)
url=u'ws://{hostname}:8081/ws',

我想知道我是否遗漏了WAMP连接的一些明显的东西,或者这可能是我试图连接的crossbar实例上的配置问题。在


Tags: 端口fromimportselfnoneconfigifws

热门问题