扭曲的UDP到TCP桥

时间:2013-11-15 18:40:17

标签: python tcp udp twisted tunnel

最近,我第一次尝试使用Twisted / Python构建一个应用程序,它将传入的UDP字符串从TCP端口回送出去。我认为这很简单,但我无法让它工作。下面的代码是示例TCP& UDP服务器已修改为一起运行。我只是想在两者之间传递一些数据。任何帮助将不胜感激。

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

class TCPServer(Protocol):

    def dataReceived(self, data):
        self.transport.write(data)


class UDPServer(DatagramProtocol):

    def datagramReceived(self, datagram, address):
        #This is where I would like the TCPServer's dataReceived method run passing "datagram".  I've tried: 
        TCPServer.dataReceived(datagram)
        #But of course that is not the correct call because UDPServer doesn't recognize "dataReceived"


def main():
    f = Factory()
    f.protocol = TCPServer
    reactor.listenTCP(8000, f)
    reactor.listenUDP(8000, UDPServer())
    reactor.run()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

这基本上是经常被问到的How do I make input on one connection result in output on another?

此问题中的UDP< - > TCP细节不会破坏FAQ条目中给出的一般答案。请注意,DatagramProtocolProtocol更容易使用,因为您已经拥有DatagramProtocol实例,而无需像Protocol那样获得工厂的合作} case。

换句话说:

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

class TCPServer(Protocol):
    def connectionMade(self):
        self.port = reactor.listenUDP(8000, UDPServer(self))

    def connectionLost(self, reason):
        self.port.stopListening()


class UDPServer(DatagramProtocol):
    def __init__(self, stream):
        self.stream = stream

    def datagramReceived(self, datagram, address):
        self.stream.transport.write(datagram)


def main():
    f = Factory()
    f.protocol = TCPServer
    reactor.listenTCP(8000, f)
    reactor.run()

if __name__ == '__main__':
    main()

请注意基本更改:UDPServer需要在TCPServer的实例上调用方法,因此需要对该实例的引用。这是通过使TCPServer实例将自身传递给UDPServer初始值设定项并使UDPServer初始化程序将该引用保存为UDPServer实例的属性来实现的。