Python Broadcast senden

Aus Ethersex_Wiki
Wechseln zu: Navigation, Suche

Broadcast senden

from socket import *

s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET,SO_BROADCAST,1)
s.sendto('Broadcast test',('255.255.255.255',20000))
s.close()

Broadcast empfangen

from socket import *

host = ""

port = 20000
buf = 1024
addr = (host,port)

UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

while 1:
 data,addr = UDPSock.recvfrom(buf)
 if not data:
  print "Client has exited!"
  break
 else:
  print "\
Received message '", data,"'"

UDPSock.close()