HI
我想通过加入Multicast组来捕获UDP数据包。在收到数据包之后,我想检查该UDP数据包的TTL值。我如何通过使用python来实现这一目标?
如下所述的Sammple代码: 这里 rec_port 是我用来绑定的任何端口;例如:9180 rec_hostname 是我加入的任何多播组;例如:239.2.2.2
#! /usr/bin/env python
.........
............
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", rec_port))
mreq = struct.pack("4sl", socket.inet_aton(rec_hostname), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
total_length = 0
while True:
print "Waiting for the packets, if no packets recevied by 10 seconds, than i will exit"
sock.settimeout(10)
data , address = sock.recvfrom(2048)
if len(data) > 1:
total_length=total_length + len(data)
print "Data is:", data
print "Length of data received is:", len(data)
print "Packet recevied from :", address[0]
print "Total Packet size:", total_length
else:
break
sock.close()
print "Total Packet size:", total_length