123456789101112131415161718#!/usr/bin/python python # -*- coding:UTF-8 -*-# UDP 广播接收from socket import *HOST = '0.0.0.0'PORT = 6681BUFSIZE = 1024broadcastrecv = socket(AF_INET, SOCK_DGRAM)broadcastrecv.bind((HOST,PORT))print('wating...')while True: data, addr = broadcastrecv.recvfrom(BUFSIZE) print('recv: %s %s'%(addr,data) ) broadcastrecv.close() 123456789101112131415161718192021#!/usr/bin/python python # -*- coding:UTF-8 -*-# UDP广播发送from socket import *HOST = '192.168.3.255'PORT = 6681BUFSIZE = 1024ADDR = (HOST, PORT)broadcast = socket(AF_INET, SOCK_DGRAM)broadcast.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)while True: data = raw_input('>') if not data: break print("send: %s"%data) broadcast.sendto(data, ADDR) broadcast.close()