解决了:)
因此,显然IPv6 不会 侦听所有接口的多播。这个语法
mreq = group + struct.pack('@I', 0)错了。根据这个,MREQ由该组ID,接口ID,其中0是默认接口(在我的病例WIFI)的。为了侦听来自其他接口的多播,应指定网络接口索引。
网络接口索引是
%在运行ipconfig时在ipv6地址中的thet之后出现的数字,也可以在cmd中运行“ route print”时找到。
我使用以下代码在python上找到了它:
import netifaces as niimport _winreg as wr # use "winreg" in python3def get_ethernet_ipv6_ifindex(): x=ni.interfaces() con_names=get_connection_name_from_guid(x) ethernet_index= con_names.index('Ethernet') addresses= ni.ifaddresses(x[ethernet_index]) brod_addr=addresses[socket.AF_INET6][-1]["broadcast"] return int(brod_addr[brod_addr.find("%")+1:])"""Taken from the very helpful https://stackoverflow.com/questions/29913516/how-to-get-meaningful-network-interface-names-instead-of-guids-with-netifaces-un"""def get_connection_name_from_guid(iface_guids): iface_names = ['(unknown)' for i in range(len(iface_guids))] reg = wr.ConnectRegistry(None, wr.HKEY_LOCAL_MACHINE) reg_key = wr.OpenKey(reg, r'SYSTEMCurrentControlSetControlNetwork{4d36e972-e325-11ce-bfc1-08002be10318}') for i in range(len(iface_guids)): try: reg_subkey = wr.OpenKey(reg_key, iface_guids[i] + r'Connection') iface_names[i] = wr.QueryValueEx(reg_subkey, 'Name')[0] except WindowsError: pass return iface_names接着-
mreq = group + struct.pack('@I', get_ethernet_ipv6_ifindex())


