1.导入套接字
from socket import *
2.创建UDP套接字
udp_socket = socket(AF_INET, SOCK_DGRAM)
3.绑定地址和端口
dest = ("<broadcast>", 8080)
4.设置套接字选项
udp_socket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
5.发送广播数据
udp_socket.sendto("这是广播数据".encode("gb2312"), dest)
6.接收广播返回数据
(data, address) = udp_socket.recvfrom(2048)
print("Recieve From %s : %s"%(address, data))
来源:oschina
链接:https://my.oschina.net/u/2483040/blog/1556863