-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_server.py
More file actions
41 lines (28 loc) · 963 Bytes
/
Copy pathrun_server.py
File metadata and controls
41 lines (28 loc) · 963 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
29
30
31
32
33
34
35
36
37
38
39
40
41
import socket
import struct
import sys
message = 'MultiCast'
multicast_group = ('224.3.29.71', 10000)
# Create the datagram socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Set a timeout so the socket does not block indefinitely when trying
# to receive data.
sock.settimeout(0.2)
# Set the time-to-live for messages to 1 so they do not go past the
# local network segment.
ttl = struct.pack('b', 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
encoded_message = str.encode(message)
send_message = sock.sendto(encoded_message, multicast_group)
while True:
print("waiting")
try:
print("send Message")
send_message = sock.sendto(encoded_message, multicast_group)
data, server = sock.recvfrom(16)
except socket.timeout:
print('timed out, no more responses')
else:
print("Got a response")
print("closing socket")
sock.close()