Python Broadcast senden

Aus Ethersex_Wiki
Version vom 28. März 2009, 21:22 Uhr von Stettberger (Diskussion | Beiträge) (Die Seite wurde neu angelegt: == Broadcast senden == <pre> from socket import * s = socket(AF_INET, SOCK_DGRAM) s.setsockopt(SOL_SOCKET,SO_BROADCAST,1) s.sendto('Broadcast test',('255.255.255.255...)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
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()