以下是Twisted提供的处理接收组播的代码示例。我目前正在使用同一个客户端监听多个组,并希望能够打印出某个数据包来自哪个组。我认为这可以从datagramReceived的地址参数中接收到,但是这只给我一个包含本地IP和端口的元组,而不是组自身的地址。
问题:如何在Twisted协议/API中打印数据报来源的多播地址?
问题:如何在Twisted协议/API中打印数据报来源的多播地址?
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
class MulticastPingClient(DatagramProtocol):
def startProtocol(self):
# Join the multicast address, so we can receive replies:
self.transport.joinGroup("228.0.0.5")
self.transport.joinGroup("229.0.2.11")
self.transport.joinGroup("221.3.3.3")
# Send to 228.0.0.5:8005 - all listeners on the multicast address
# (including us) will receive this message.
self.transport.write('Client: Ping', ("228.0.0.5", 8005))
def datagramReceived(self, datagram, address):
print "Datagram %s received from %s" % (repr(datagram), repr(address))
reactor.listenMulticast(8005, MulticastPingClient(), listenMultiple=True)
reactor.run()