Home >Backend Development >Python Tutorial >python基于socket实现网络广播的方法

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-10 15:13:491511browse

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

import socket, sys
dest = ('<broadcast>', 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程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn