小编Non*_*ame的帖子

通过UDP回送通过不同TCP端口回收的消息

我使用Python 2.7.x(2.7.8)并尝试使用twisted python编写程序,如下所示:

  • 程序等待通过TCP 7001或UDP 7000接收的消息.
  • 通过UDP 7000接收的消息将桥接到TCP 7001.

我无法弄清楚如何将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)

python sockets networking udp twisted

5
推荐指数
1
解决办法
431
查看次数

标签 统计

networking ×1

python ×1

sockets ×1

twisted ×1

udp ×1