发送端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21import time import socket # 组播组IP和端口 mcast_group_ip = '234.2.2.2' mcast_group_port = 23456 def sender(): # 建立发送socket,和正常UDP数据包没区别 send_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # 每十秒发送一遍消息 while True: message = "this message send via mcast !" # 发送写法和正常UDP数据包的还是完全没区别 # 猜测只可能是网卡自己在识别到目的ip是组播地址后,自动将目的mac地址设为多播mac地址 send_sock.sendto(message.encode(), (mcast_group_ip, mcast_group_port)) print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: message send finish') time.sleep(2) if __name__ == "__main__": sender()
接收端
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33import struct import time import socket # 组播组IP和端口 mcast_group_ip = '234.2.2.2' mcast_group_port = 23456 def receiver(): # 建立接收socket,和正常UDP数据包没区别 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # 获取本地IP地址 local_ip = socket.gethostbyname(socket.gethostname()) # 监听端口,已测试过其实可以直接bind 0.0.0.0;但注意不要bind 127.0.0.1不然其他机器发的组播包就收不到了 sock.bind((local_ip, mcast_group_port)) # 加入组播组 mreq = struct.pack("=4sl", socket.inet_aton(mcast_group_ip), socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP,socket.IP_ADD_MEMBERSHIP,mreq) # 允许端口复用,看到很多教程都有没想清楚意义是什么,我这里直接注释掉 # sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 设置非阻塞,看到很多教程都有也没想清楚有什么用,我这里直接注释掉 # sock.setblocking(0) while True: try: message, addr = sock.recvfrom(1024) print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: Receive data from {addr}: {message.decode()}') except : print("while receive message error occur") if __name__ == "__main__": receiver()
最后
以上就是粗犷帅哥最近收集整理的关于python组播通信的全部内容,更多相关python组播通信内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复