0

我正在尝试使用带有高速公路的 Python [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)

我在我的本地主机上运行了一个交叉开关实例进行测试,当我访问它时,一切正常。

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected
2016-04-01T17:26:16+0000 session attached
(stuff happens here, events get published, until max is reached)
2016-04-01T17:26:19+0000 Received SIGINT, shutting down.
2016-04-01T17:26:19+0000 disconnected
2016-04-01T17:26:19+0000 Main loop terminated.

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

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 连接的明显内容,或者这可能是我尝试连接的交叉开关实例上的配置问题。

4

1 回答 1

0

我通过在具有以下高速公路和扭曲版本的 virtualenv 上运行它来解决我的问题:

autobahn==0.10.2
Twisted==15.1.0

我不知道为什么最新版本会这样,我只知道我上面的代码只适用于这些版本。

于 2016-04-01T19:35:35.023 回答