2011-12-30 85 views
1

我有以下tcpserver简单的例子。我期待与udp服务器共享因子counter var,所以在每次连接时它都会为tcp和udp增加值。因此如果我用TCP首先连接这将是2,那么,如果我连接上的UDP端口..这将是3扭曲共享tcp/udp协议之间的变量

#!/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类,将沿侧工作..那是我的难点。我想我如何引用计数器确定,将工作

回答

2

reactor.listenUDP的参数应该是一个DatagramProtocol实例,如在UDP例如: http://twistedmatrix.com/documents/current/core/howto/udp.html。您不能将您的QOTDFactory用于UDP,因此它不需要TCP vs 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() 

我改名TCPUDPStreamCounterDatagramCounter,因为他们不局限于分别TCP和UDP使用(和那些是不可怕的描述性名称)。例如,您也可以使用reactor.listenSSL通过SSL使用StreamCounter

+0

DatagramCounter如何共享对TCP服务器使用的因子的引用? – kratsg 2015-10-19 21:07:45

+0

优秀的问题!我的回答其实是错误的。我忘了添加将DatagramCounter挂接到工厂的部分。编辑答案与修复。 – 2016-01-13 18:31:55

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 
+0

对不起,也许这个问题是措辞错误..但我觉得我的self.counter是如何引用它在TCP类确定..我的问题是启动基于关闭该工厂 – Mike 2011-12-30 17:00:26

+0

好的另一个UDP类。但老实说我还是不明白。你能在制定这个问题时更精确一些吗?!什么_exactly_是你坚持的问题? – gecco 2011-12-30 21:20:05

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()