这是来自 Twisted 的以下代码示例,用于处理接收多播。我目前正在收听具有相同客户端的许多组,并且我希望能够打印出某个数据报包来自哪个组。我认为这可以从 datagramReceived 的地址参数中接收;但是,这只给了我一个包含组绑定到的本地 ip 和端口的元组,而不是组本身的地址。
问题:如何在 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()