-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcsr.py
31 lines (26 loc) · 867 Bytes
/
mcsr.py
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
import sys, struct, socket
def mcast_server(addr, port):
fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# bind udp port
fd.bind(('', port))
# set mcast group
mreq = struct.pack('4sl', socket.inet_aton(addr), socket.INADDR_ANY)
fd.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
while 1:
data, addr = fd.recvfrom(1024)
print '%s bytes from %s: %s' % (len(data), addr, data)
except KeyboardInterrupt:
print 'done'
sys.exit(0)
if __name__ == '__main__':
try:
addr = sys.argv[1]
port = int(sys.argv[2])
except IndexError:
addr = '225.0.0.1'
port = 1905
finally:
print 'running server on %s:%d' % (addr, port)
mcast_server(addr, port)