簡體   English   中英

Twisted在tcp / udp協議之間共享一個變量

[英]Twisted share a variable between tcp/udp protocols

我有以下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類...這是我的關鍵點。 我想我如何引用計數器是好的,並將工作

reactor.listenUDP的參數應該是DatagramProtocol實例,如UDP示例中所示: http//twistedmatrix.com/documents/current/core/howto/udp.html 您不能將您的QOTDFactory與UDP QOTDFactory使用,因此它不需要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為好。

這是否適合您的需求?

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

您可以使用靜態類變量來實現此計數器:

class QOTDFactory(Factory):
    counter = 1

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

        QOTDFactory.counter += 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM