在Python中发送和接收IPv6链路本地多播UDP数据报?
下面是一个简单的 IPv4 UDP 广播,然后在所有接口上进行侦听。
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
sock.bind(("", 1337))
sock.sendto("hello world", ("255.255.255.255", 1337))
while True:
data, addr = sock.recvfrom(0x100)
print "received from {0}: {1!r}".format(addr, data)
我想调整它以发送和接收 IPv4 和 IPv6。
我已经尽可能多地查阅和阅读,并相信以下大致是我需要采取的路线:
- 创建 IPv6 套接字。
- 将套接字添加到链路或站点本地多播组。
- 将 UDP 数据包发送到正在使用的组的多播地址。
我得到的进一步信息是,我可能需要绑定到多个接口,并使用 setsockopt() 告诉套接字它还应该接收多播数据包。最后,可以全面使用 getaddrinfo()
在 IPv6 不可用的情况下优雅地“回退”到 IPv4。
我已经实现了大部分内容,但主要是在多播部分上遇到了困难。最好有完整的Python代码示例,或者对所需常量和地址进行生动的描述。
This following is a straightforward IPv4 UDP broadcast, followed by listening on all interfaces.
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
sock.bind(("", 1337))
sock.sendto("hello world", ("255.255.255.255", 1337))
while True:
data, addr = sock.recvfrom(0x100)
print "received from {0}: {1!r}".format(addr, data)
I want to adjust this to send and receive both IPv4 and IPv6.
I've poked around and read as much as I can and believe the following is roughly the route I need to take:
- Create an IPv6 socket.
- Add the socket to a link or site local multicast group.
- Send the UDP packets to the multicast address for the group in use.
Further info I have is that I may need to bind to several interfaces, and tell the socket using setsockopt()
that it should also receive multicast packets. Lastly getaddrinfo()
can be used across the board to gracefully "drop back" to IPv4 where IPv6 isn't available.
I have much of this implemented, but stumble primarily on the multicasting parts. A complete code example in Python, or vivid description of the constants and addresses needed are preferred.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 python mcast demo 的链接,两者都可以IPv4 和 IPv6。
Here's a link to python mcast demo, does both IPv4 and IPv6.
我目前正在此处提出一个涉及获取多播的问题收到消息的地址,但源代码回答了您的问题!
收听:
和发送:
这是针对 python3.6 的,对于 python 2.7,我认为编码不是必需的。同样在 struct.pack 行中,我看到了“16s15s”的变体,例如“4s”,但我不知道它是什么以及我为我工作了什么!
I'm currently asking a question over here that involves getting the multicast address of a received message, but the source code answers your question!
To listen:
and to send:
This is for python3.6, with python 2.7 I don't think the encodes are necessary. Also in the struct.pack line, I've seen variations of "16s15s" such as "4s", but I don't know what it is and what I have worked for me!