使用 Twisted 与服务器交互?
我一直在尝试一些 Twisted 并且遇到了一个问题。我正在实现几个可以使用 telnet localhost x 连接到的服务器,
我的代码如下处理:
reactor.listenTCP(12001, server1, ....)
reactor.listenTCP(12002, server2, ....)
然后
,这些使用我的工厂来构建一个协议
,我想知道如何让这些服务器彼此交互。例如,假设客户端发送请求来更新每个服务器上通用的值,并且我想将该值中继到其他服务器以更新其当前值。我研究了 reactor.connectTCP
但这似乎没有达到我想要的效果。有没有一种方法可以在不使用 telnet 命令的情况下连接到我的服务器?
I have been dabling in some Twisted and I've come across a problem. I'm implementing a couple servers that can be connected to using telnet localhost x
My code handles it like this:
reactor.listenTCP(12001, server1, ....)
reactor.listenTCP(12002, server2, ....)
etc.
These then use my factory to build a protocol
I'm wondering how I can get these servers to interact with each other. For example, let's say a client sends a request to update a value common across each server and I want to relay this value to the other servers to update their current value. I looked into reactor.connectTCP
but this doesn't seem to do what I want. Is there a way to connect to my servers without using the telnet command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然。为此,请使用普通的 Python 技术。让我们以正在更新的值为例。我将使它成为一个简单的计数器,可以通过与一个服务器的连接以及发送到另一台服务器的连接的值来递增:
两个工厂现在共享一段可变状态。协议可以访问它们的工厂,因为默认的 ServerFactory 有助于将自身设置为它实例化的协议上的属性,因此协议也可以达到工厂上的共享可变状态。一个增加计数器,另一个检索值。
您可以通过对非协议、非工厂对象的共享状态和方法调用来构建许多这样的场景。
Sure. Use normal Python techniques for this. Let's take your example of a value being updated. I'll make it a simple counter that might be incremented by connections to one server and the value sent out to connections to the other server:
The two factories are now sharing a piece of mutable state. The protocols can access their factories because the default
ServerFactory
helpfully sets itself as an attribute on protocols it instantiates, so the protocols can also reach the shared mutable state on the factories. One increments the counter, the other retrieves the value.You can construct many scenarios like this with shared state and method calls onto non-protocol, non-factory objects.