我有一些代碼,通過UDP多播監聽「通知」。我可以獲取發件人的IP地址,但我真正需要的是發件人的MAC地址(因爲IP地址可以並將會更改)。在Python中查找組播UDP消息發送者的MAC地址?
有沒有一種簡單的方法在Python中做到這一點?
包含代碼段供參考,但可能不必要。
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Allow multiple sockets to use the same PORT number
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind to the port that we know will receive multicast data
sock.bind((self.interface, MCAST_PORT))
# Tell API we are a multicast socket
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
# Tell API we want to add ourselves to a multicast group
# The address for the multicast group is the third param
status = sock.setsockopt(socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(MCAST_ADDR) + socket.inet_aton(self.interface));
data, addr = sock.recvfrom(1024)
...
應該可以用'scapy'的L2插座雖然可能需要一些工作:http://www.secdev.org/projects/scapy /doc/usage.html – Pierz 2016-12-07 18:10:15