使用 Twisted 与服务器交互?

发布于 2024-10-20 07:53:57 字数 395 浏览 1 评论 0原文

我一直在尝试一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2024-10-27 07:53:57

当然。为此,请使用普通的 Python 技术。让我们以正在更新的值为例。我将使它成为一个简单的计数器,可以通过与一个服务器的连接以及发送到另一台服务器的连接的值来递增:

from twisted.internet import reactor
from twisted.internet.protocol import ServerFactory, Protocol

class Counter(object):
    def __init__(self):
        self._value = 0

    def increment(self):
        self._value += 1

    def get(self):
        return self._value


class Incrementor(Protocol):
    def connectionMade(self):
        self.factory.value.increment()
        self.transport.loseConnection()


class Reporter(Protocol):
    def connectionMade(self):
        self.transport.write("Value is %d\n" % (self.factory.value.get(),))
        self.transport.loseConnection()

server1 = ServerFactory()
server1.protocol = Incrementor

server2 = ServerFactory()
server2.protocol = Reporter

server1.value = server2.value = Value()

reactor.listenTCP(12001, server1)
reactor.listenTCP(12002, server2)
reactor.run()

两个工厂现在共享一段可变状态。协议可以访问它们的工厂,因为默认的 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:

from twisted.internet import reactor
from twisted.internet.protocol import ServerFactory, Protocol

class Counter(object):
    def __init__(self):
        self._value = 0

    def increment(self):
        self._value += 1

    def get(self):
        return self._value


class Incrementor(Protocol):
    def connectionMade(self):
        self.factory.value.increment()
        self.transport.loseConnection()


class Reporter(Protocol):
    def connectionMade(self):
        self.transport.write("Value is %d\n" % (self.factory.value.get(),))
        self.transport.loseConnection()

server1 = ServerFactory()
server1.protocol = Incrementor

server2 = ServerFactory()
server2.protocol = Reporter

server1.value = server2.value = Value()

reactor.listenTCP(12001, server1)
reactor.listenTCP(12002, server2)
reactor.run()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文