python socket udp广播
from socket import * import time HOST = ‘<broadcast>‘ PORT = 9090 BUFSIZE = 1024 ADDR = (HOST, PORT) udpCliSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udpCliSock.bind((‘‘, 0)) udpCliSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
count=10 while count>=0: count-=1 data=‘SSTOP&tuling‘ if not data: break print "sending -> %s"%data udpCliSock.sendto(data,ADDR) time.sleep(1) udpCliSock.close() exit(0)
Python socket 监听udp广播
import socket
import time
#初始化udp s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
#设置端口可以复用 s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((‘‘,udpPort))
count=10 while count>=0:
count-=1 data,addr=s.recvfrom(1024)
time.sleep(1)
s.close()
exit(0)
原文:http://www.cnblogs.com/dyan1024/p/7735769.html