-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver.py
More file actions
40 lines (30 loc) · 998 Bytes
/
receiver.py
File metadata and controls
40 lines (30 loc) · 998 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
#
# mostly from
# http://bioportal.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/socket/multicast.html
#
import socket
import struct
import sys
import json
multicast_group = '224.3.29.71'
server_address = ('', 10000)
# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind to the server address
sock.bind(server_address)
# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# Receive/respond loop
while True:
# print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(1024)
try:
print >> sys.stderr, json.loads( data )
except:
print >> sys.stderr, 'falied to decode from' %(address, )
# print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
# print >>sys.stderr, data
print >>sys.stderr, ""