当我在连接以太网电缆的Raspberry Pi 3上运行此片段时,它可以工作并在UDP多播上发送消息但是当以太网断开连接时会显示错误消息
import socket
from socket import IPPROTO_IPV6
import struct
PORT = 26000
ADDR = 'ff01::1' #IPV6 Multicast Address
addrInfo = socket.getaddrinfo(ADDR, None)[0]
mcast_sock = socket.socket(addrInfo[0], socket.SOCK_DGRAM)
mcast_sock.setsockopt(IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 1)
mcast_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mcast_sock.bind(('', PORT))
#Join Multicast grp.
group = socket.inet_pton(addrInfo[0], addrInfo[4][0])
mreq = group + struct.pack('@I', 0)
mcast_sock.setsockopt(IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
mcast_sock.sendto(b"Message", (ADDR, PORT))
仅Wlan0接口启动时的错误消息
Traceback (most recent call last):
File "test.py", line 20, in <module>
mcast_sock.sendto(b"Message", (ADDR, PORT))
socket.error: [Errno 99] Cannot assign requested address
的ifconfig:
eth0 Link encap:Ethernet HWaddr b8:27:eb:13:e8:41
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:16716 errors:0 dropped:7224 overruns:0 frame:0
TX packets:408 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1017246 (993.4 KiB) TX bytes:56739 (55.4 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
wlan0 Link encap:Ethernet HWaddr b8:27:eb:46:bd:14
inet addr:10.90.128.72 Bcast:10.90.128.255 Mask:255.255.255.0
inet6 addr: fe80::a4dd:c183:1f7a:5f43/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:58481 errors:0 dropped:41915 overruns:0 frame:0
TX packets:5196 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12027273 (11.4 MiB) TX bytes:925411 (903.7 KiB)
答案 0 :(得分:0)
seems that the local routing favored eth0 over wlan0
ip -6 route show table local
local ::1 dev lo proto kernel metric 256
local ::1 dev lo proto none metric 0
local fe80::a4dd:c183:1f7a:5f43 dev lo proto none metric 0
ff00::/8 dev eth0 metric 256
ff00::/8 dev wlan0 metric 256
Found this solution
How to send multicast packets via a specfic interface in Linux
so I ran the following command:
ip -6 route add ff01::/16 dev lo table local
Then it works :)