broadcast.py 724 B

123456789101112131415161718192021222324
  1. import socket
  2. import threading
  3. class Broadcast():
  4. def __init__(self,broadcast, broadcastPort):
  5. msg = bytes("Hello, Who can do somthing for me? Tnx",'utf-8')
  6. self.onlineOracles=[]
  7. self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  8. self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  9. self.sock.sendto(msg,(broadcast, broadcastPort))
  10. t1 = threading.Thread(target=self.listen)
  11. t1.start()
  12. print("Broadcast sended")
  13. def listen(self):
  14. while True:
  15. msg, addr = self.sock.recvfrom(32)
  16. self.onlineOracles.append((addr[0],msg.decode("utf-8")))
  17. # print("message",msg," from ",addr)