在 buildProtocol 期间检索监听端口

发布于 2024-10-04 05:29:05 字数 490 浏览 2 评论 0原文

我正在修改 ServerFactory 的 buildProtocol 方法,基本上工厂侦听端口 11000 和 12000,并且我有两个协议,每个端口一个。我正在尝试检索客户端用于侦听的端口,以便我可以实例化正确的协议。

前任。客户端侦听端口 11000,协议 1 被实例化。客户端侦听端口 12000,协议 2 被实例化。

我认为这只能在 buildProtocol 阶段完成,有没有办法确定使用哪个端口进行连接? buildProtocol使用的地址参数是客户端地址,我需要服务器端口。

伪代码:

def buildProtocol(self, address):
  if address connects at port 11000:
    proto = TransformProtocol()
  else:
    proto = TransformProtocol2()
  proto.factory = self
  return proto

i'm modifying the ServerFactory's buildProtocol method, basically the factory listens in at port 11000 and 12000, and I have two protocols, one for port each port. I'm trying to retrieve the port that the client used to listen in so that I can instantiate the correct protocol.

ex. client listens in at port 11000, protocol 1 is instantiated. client listens in at port 12000, protocol 2 is instantiated.

i think this can be only done in the buildProtocol stage, is there a way to determine which port was used to connect? the address parameter used by buildProtocol is the client address, I need the server port.

pseudo code:

def buildProtocol(self, address):
  if address connects at port 11000:
    proto = TransformProtocol()
  else:
    proto = TransformProtocol2()
  proto.factory = self
  return proto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

゛时过境迁 2024-10-11 05:29:05

我认为,您可能需要使用 Twisted 服务:
链接文本

你的代码会变成这样:

from twisted.application import internet, service
from twisted.internet import protocol, reactor
from twisted.protocols import basic

class UpperCaseProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        self.transport.write(line.upper() + '\r\n')
        self.transport.loseConnection()

class LowerCaseProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        self.transport.write(line.lower() + '\r\n')
        self.transport.loseConnection()

class LineCaseService(service.Service):

    def getFactory(self, p):
        f = protocol.ServerFactory()
        f.protocol = p
        return f

application = service.Application('LineCase')
f = LineCaseService()

serviceCollection = service.IServiceCollection(application)
internet.TCPServer(11000,f.getFactory(UpperCaseProtocol)).setServiceParent(serviceCollection)
internet.TCPServer(12000,f.getFactory(LowerCaseProtocol)).setServiceParent(serviceCollection)

但是,这里我们有两个工厂实例。

I think, you may need to use Twisted services:
link text

You code become something like:

from twisted.application import internet, service
from twisted.internet import protocol, reactor
from twisted.protocols import basic

class UpperCaseProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        self.transport.write(line.upper() + '\r\n')
        self.transport.loseConnection()

class LowerCaseProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        self.transport.write(line.lower() + '\r\n')
        self.transport.loseConnection()

class LineCaseService(service.Service):

    def getFactory(self, p):
        f = protocol.ServerFactory()
        f.protocol = p
        return f

application = service.Application('LineCase')
f = LineCaseService()

serviceCollection = service.IServiceCollection(application)
internet.TCPServer(11000,f.getFactory(UpperCaseProtocol)).setServiceParent(serviceCollection)
internet.TCPServer(12000,f.getFactory(LowerCaseProtocol)).setServiceParent(serviceCollection)

But, here we have two factory instances.

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