扭曲视角经纪人与高速公路

2024-10-03 13:17:36 发布

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

我已经离开了我的舒适区,我有点困惑我该怎么登录。

Autobahn提供了一种使用RPC调用Python服务器的方法,但是Twisted只提供了如何为客户机使用Twisted的文档。

为了正确解释我的意思,下面是代码:

在Javascript中,可以像这样调用RPC函数: sess.call(url, user, pass)

Python客户端代码:

from twisted.spread import pb
from twisted.internet import reactor
from twisted.cred import credentials

class Client(pb.Referenceable):

    def remote_print(self, message):
        print message

    def connect(self):
        factory = pb.PBClientFactory()
        reactor.connectTCP("localhost", 8800, factory)
        def1 = factory.login(credentials.UsernamePassword("Bob", "secret"),
                             client=self)
        def1.addCallback(self.connected)
        reactor.run()

    def connected(self, perspective):
        self.perspective = perspective

        d = perspective.callRemote("joinGroup", "#Magic")
        d.addCallback(self.gotGroup)

    def gotGroup(self, group):
        d = group.callRemote("send", "Test message.")
        d.addCallback(self.shutdown)

    def shutdown(self, result):
        reactor.stop()


Client().connect()

Python服务器代码:

^{pr2}$

基本上,我只需要找出如何通过RPC在Javascript中访问登录调用,但我不知道如何做到这一点,Google也没有为我提供任何选项。


Tags: 代码fromimportself服务器messagefactorydef
1条回答
网友
1楼 · 发布于 2024-10-03 13:17:36

Autobahn project不仅提供WebSocket的实现(其中之一是AutobahnPython),而且还提供WAMP (The WebSocket Application Messaging Protocol)

WAMP is an open WebSocket subprotocol that provides two asynchronous messaging patterns: RPC and PubSub.

WAMP同样得到了AutobahnPython和AutobahnJS(以及越来越多的其他实现。。见here)。似乎带有高速公路的高速公路可以让你做你想做的事情:

  • 通过WebSocket从浏览器中运行的JavaScript调用服务器上的远程过程(用Python/Twisted/AutobahnPython编写)
  • 在代理使用Python/Twisted/AutobahnPython(或其他语言)的情况下,再次从JavaScript发布和订阅

免责声明:我是《高速公路和WAMP》的原作者,为塔文多工作。在

在高速公路站点和相应的GitHub repo中有RPC和PubSub的多个完整的工作示例,例如here。在

相关问题 更多 >