我使用Python 2.7.x(2.7.8)并尝试使用twisted python编写程序,如下所示:
我无法弄清楚如何将UDP桥接到TCP,所以我在这个站点上查了一个例子,就像这个,Twisted UDP to TCP Bridge,但问题是该例子因为它不起作用而撒谎.我在datagramReceived上添加了一个"打印数据报",以查看UDP是否响应接收任何内容,而不是.这完全令人沮丧.
这是我当前的测试代码稍微改变了一下这个例子:
from twisted.internet.protocol import Protocol, Factory, DatagramProtocol
from twisted.internet import reactor
class TCPServer(Protocol):
def connectionMade(self):
self.port = reactor.listenUDP(7000, UDPServer(self))
def connectionLost(self, reason):
self.port.stopListening()
def dataReceived(self, data):
print "Server said:", data
class UDPServer(DatagramProtocol):
def __init__(self, stream):
self.stream = stream
def datagramReceived(self, datagram, address):
print datagram
self.stream.transport.write(datagram)
def main():
f = Factory()
f.protocol = TCPServer
reactor.listenTCP(7001, f)
reactor.run()
if __name__ …Run Code Online (Sandbox Code Playgroud)