【发布时间】:2013-11-29 06:30:41
【问题描述】:
最近,我第一次尝试使用 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()
【问题讨论】:
标签: python tcp udp twisted tunnel