python基于socket实现网络广播的方法

本文实例讲述了python基于socket实现网络广播的方法。分享给大家供大家参考。具体实现方法如下:

import socket, sys
dest = (”, 51423)
s = socket.socket(socket.af_inet, socket.sock_dgram)
s.setsockopt(socket.sol_socket, socket.so_broadcast,1)
s.sendto(“hi”, dest)
print “looking for replies; press ctrl-c to stop.”
while 1:
(buf, address) = s.recvfrom(2048)
if not len(buf):
break
print “received from %s: %s” % (address, buf)

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论