首页 > 编程语言 > 详细

python socket 广播

时间:2017-10-26 12:32:45      阅读:246      评论:0      收藏:0      [点我收藏+]

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)

 

python socket 广播

原文:http://www.cnblogs.com/dyan1024/p/7735769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!