Python 本地发送udp广播

from socket import *

    HOST = ''
    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

你可能感兴趣的:(Python 本地发送udp广播)