-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP_Broadcaster.py
More file actions
28 lines (15 loc) · 790 Bytes
/
UDP_Broadcaster.py
File metadata and controls
28 lines (15 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import socket
def transmit():
interfaces = socket.getaddrinfo(host=socket.gethostname(), port=None, family=socket.AF_INET)
allips = [ip[-1][0] for ip in interfaces]
# create a socket object
transmitter = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP
broadast_Port = 8002
broadcast_addr = (socket.inet_aton('255.255.255.255'),broadast_Port)
transmitter.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1023)
transmitter.bind((allips[0], 0)) #open an outgoing port on first interface. Don't care about port number :)
print(f"Read to TX on {broadcast_addr}")
while True:
tx = input("What to send?").strip().encode() # convert string to bytes
transmitter.sendto(tx,broadcast_addr)
transmit()