Python Twisted在tcp/udp协议之间共享一个变量

Python Twisted在tcp/udp协议之间共享一个变量,python,twisted,Python,Twisted,我有下面的tcpserver简单示例。我希望与udp服务器共享因子计数器var,以便在每次连接时,它都将包含tcp和udp的值。因此,如果我首先使用tcp连接,它将是2,然后如果我连接到udp上的端口。。3点 #!/usr/bin/env python from twisted.internet.protocol import Factory, Protocol from twisted.internet import reactor class TCP(Protocol): de

我有下面的tcpserver简单示例。我希望与udp服务器共享因子计数器var,以便在每次连接时,它都将包含tcp和udp的值。因此,如果我首先使用tcp连接,它将是2,然后如果我连接到udp上的端口。。3点

#!/usr/bin/env python

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

class TCP(Protocol):

    def connectionMade(self):
        self.factory.counter += 1
        self.transport.write(str(self.factory.counter)+'\r\n')
        self.transport.loseConnection()

class QOTDFactory(Factory):

    def __init__(self, protocol='tcp'):
        if protocol == 'tcp':
            self.protocol = TCP
        else:
            self.protocol = UDP

        self.counter = 1

reactor.listenTCP(8007, QOTDFactory('tcp'))
#reactor.listenUDP(8007, QOTDFactory('udp'))

reactor.run()
我的主要问题是启动一个UDP类,该类将同时工作。。这是我的症结所在。我认为我如何引用计数器是可以的,并且会起作用

您可以使用一个来实现此计数器:

class QOTDFactory(Factory):
    counter = 1

    def __init__(self, protocol='tcp'):
        if protocol == 'tcp':
            self.protocol = TCP
        else:
            self.protocol = UDP

        QOTDFactory.counter += 1
您可以使用来实现此计数器:

class QOTDFactory(Factory):
    counter = 1

    def __init__(self, protocol='tcp'):
        if protocol == 'tcp':
            self.protocol = TCP
        else:
            self.protocol = UDP

        QOTDFactory.counter += 1

reactor.listenUDP
的参数应该是一个
DatagramProtocol
实例,如UDP示例所示:。您不能将
QOTDFactory
与UDP一起使用,因此它不需要TCP与UDP选择逻辑。相反,只需使用所需的协议逻辑创建一个
DatagramProtocol
子类,并让它共享对TCP服务器使用的工厂的引用

#!/usr/bin/env python

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

class StreamCounter(Protocol):
    def connectionMade(self):
        self.factory.counter += 1
        self.transport.write(str(self.factory.counter)+'\r\n')
        self.transport.loseConnection()


class DatagramCounter(DatagramProtocol):
    def __init__(self, factory):
        self.factory = factory

    def datagramReceived(self, data, address):
        self.factory.counter += 1
        self.transport.write(str(self.factory.counter), address)


class QOTDFactory(Factory):
    counter = 0
    protocol = StreamCounter


factory = QOTDFactory()
reactor.listenTCP(8007, factory)
reactor.listenUDP(8007, DatagramCounter(factory))

reactor.run()

我将
TCP
UDP
重命名为
StreamCounter
DatagramCounter
,因为它们不局限于分别与TCP和UDP一起使用(这些都不是可怕的描述性名称;)。例如,您也可以使用
reactor.listenSSL
通过SSL使用
StreamCounter

reactor.listenUDP的参数应该是
DatagramProtocol
实例,如UDP示例所示:。您不能将
QOTDFactory
与UDP一起使用,因此它不需要TCP与UDP选择逻辑。相反,只需使用所需的协议逻辑创建一个
DatagramProtocol
子类,并让它共享对TCP服务器使用的工厂的引用

#!/usr/bin/env python

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

class StreamCounter(Protocol):
    def connectionMade(self):
        self.factory.counter += 1
        self.transport.write(str(self.factory.counter)+'\r\n')
        self.transport.loseConnection()


class DatagramCounter(DatagramProtocol):
    def __init__(self, factory):
        self.factory = factory

    def datagramReceived(self, data, address):
        self.factory.counter += 1
        self.transport.write(str(self.factory.counter), address)


class QOTDFactory(Factory):
    counter = 0
    protocol = StreamCounter


factory = QOTDFactory()
reactor.listenTCP(8007, factory)
reactor.listenUDP(8007, DatagramCounter(factory))

reactor.run()

我将
TCP
UDP
重命名为
StreamCounter
DatagramCounter
,因为它们不局限于分别与TCP和UDP一起使用(这些都不是可怕的描述性名称;)。例如,您还可以使用reactor.listenSSL通过SSL使用
StreamCounter

这是否满足您的需要

#!/usr/bin/env python

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

class Counter():
  def __init__(self):
    self.count = 0

class TCP(Protocol):

    def connectionMade(self):
        self.factory.counter.count += 1
        self.transport.write(str(self.factory.counter)+'\r\n')
        self.transport.loseConnection()

class QOTDFactory(Factory):

    def __init__(self, protocol, counter):
        if protocol == 'tcp':
            self.protocol = TCP
        else:
            self.protocol = UDP

        self.counter = counter

counter = Counter()
reactor.listenTCP(8007, QOTDFactory('tcp', counter))
reactor.listenUDP(8007, QOTDFactory('udp', counter))

reactor.run()

这能满足你的需要吗

#!/usr/bin/env python

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

class Counter():
  def __init__(self):
    self.count = 0

class TCP(Protocol):

    def connectionMade(self):
        self.factory.counter.count += 1
        self.transport.write(str(self.factory.counter)+'\r\n')
        self.transport.loseConnection()

class QOTDFactory(Factory):

    def __init__(self, protocol, counter):
        if protocol == 'tcp':
            self.protocol = TCP
        else:
            self.protocol = UDP

        self.counter = counter

counter = Counter()
reactor.listenTCP(8007, QOTDFactory('tcp', counter))
reactor.listenUDP(8007, QOTDFactory('udp', counter))

reactor.run()

对不起,也许这个问题用词不对。。但是我认为我的self.counter在tcp类中引用它的方式是正确的。。我的问题是基于factoryOk启动另一个UDP类。但老实说我还是不明白。你能不能更精确一点来解释这个问题?!你到底在讨论什么问题?对不起,也许问题的措辞有误。。但是我认为我的self.counter在tcp类中引用它的方式是正确的。。我的问题是基于factoryOk启动另一个UDP类。但老实说我还是不明白。你能不能更精确一点来解释这个问题?!您到底遇到了什么问题?DatagramCounter如何共享对TCP服务器使用的因子的引用?好问题!我的回答实际上是错的。我忘了添加将DatagramCounter连接到工厂的部件。修改后的答案。DatagramCounter如何共享对TCP服务器使用的因子的引用?很好的问题!我的回答实际上是错的。我忘了添加将DatagramCounter连接到工厂的部件。修改了答案和修正。