gpt4 book ai didi

python - Twisted 具有多个端口、协议(protocol)和 react 器

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:50 24 4
gpt4 key购买 nike

twisted 是否支持同时使用不同的“处理程序”(每个端口的不同回调集)监听多个端口?本质上,我希望我的进程在一个进程中托管两个服务器,每个服务器执行不同的功能。我是否需要使用两个 react 器来执行此操作?

最佳答案

是的,例如,修改 quote server example您可以添加第二个实例,用不同的引号监听不同的端口:

from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

class QOTD(Protocol):

def connectionMade(self):
# self.factory was set by the factory's default buildProtocol:
self.transport.write(self.factory.quote + '\r\n')
self.transport.loseConnection()


class QOTDFactory(Factory):

# This will be used by the default buildProtocol to create new protocols:
protocol = QOTD

def __init__(self, quote=None):
self.quote = quote or 'An apple a day keeps the doctor away'

endpoint = TCP4ServerEndpoint(reactor, 8007)
endpoint.listen(QOTDFactory("configurable quote"))

endpoint2 = TCP4ServerEndpoint(reactor, 8008)
endpoint2.listen(QOTDFactory("another configurable quote"))

reactor.run()

输出:

$ nc localhost 8007
configurable quote
$ nc localhost 8008
another configurable quote

关于python - Twisted 具有多个端口、协议(protocol)和 react 器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26621431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com