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

时间:2011-12-30 14:59:03

标签: 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):

    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类......这是我的观点。我想我如何引用计数器是好的并且可以正常工作

3 个答案:

答案 0 :(得分:3)

reactor.listenUDP的参数应该是DatagramProtocol实例,如UDP示例中所示:http://twistedmatrix.com/documents/current/core/howto/udp.html。您不能将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()

我将TCPUDP重命名为StreamCounterDatagramCounter,因为它们不仅限于分别与TCP和UDP一起使用(并且这些描述并不是很糟糕)名字;)。例如,您也可以使用StreamCounter通过SSL reactor.listenSSL

答案 1 :(得分:1)

这是否适合您的需求?

#!/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()

答案 2 :(得分:0)

您可以使用static class variable来实现此计数器:

class QOTDFactory(Factory):
    counter = 1

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

        QOTDFactory.counter += 1