2
這是來自Twisted的以下代碼示例,用於處理組播接收。我現在正在用同一個客戶端收聽很多組,並希望能夠打印出某個數據報包來自哪個組。我會認爲這可以從datagramReceived的地址參數中獲得;然而,這隻給了我一個包含組綁定的本地IP和端口的元組,但不包含組本身的地址。如何從datagramReceived中獲取多播組地址?
問題:如何在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()
這樣做會調用多個線程嗎?我正在將數據報寫入沒有任何級別鎖定的數據結構。 – jab 2012-07-09 20:17:19
不,只需在reactor.run()(或者從某個事件調度的某個函數)之前多次調用listenUDP - 無需線程。 – 2012-07-10 00:58:33
很酷,謝謝! – jab 2012-07-10 14:53:12