Python 本地发送udp广播

2017-09-26  本文已影响0人  Sunnky
from socket import *

    HOST = '<broadcast>'
    PORT = 1234
    BUFSIZE = 1024

    ADDR = (HOST, PORT)

    udpCliSock = socket(AF_INET, SOCK_DGRAM)
    udpCliSock.bind(('', 0))
    udpCliSock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
    start = 0
    while start < 100:
        data = str('mac01\tA\t0\t11\t22\t33\t44').encode("utf-8")
        if not data:
            break
        print("sending -> %s" % data)
        udpCliSock.sendto(data, ADDR)
        import time
        time.sleep(1)
        start += 1
        ##    data,ADDR = udpCliSock.recvfrom(BUFSIZE)
    ##    if not data:
    ##        break
    ##    print data

    udpCliSock.close()

以上,就会发送广播,地址是:0.0.0.0 端口:1234

上一篇下一篇

猜你喜欢

热点阅读