def main(): # bind/listen on all interfaces, send with a TTL of 5 group, port = '224.1.1.2', 8053 interface = sys.argv[1] sock = mcastsocket.create_socket( ('', port), TTL=5, ) mcastsocket.join_group( sock, group=group, iface=interface, ) try: print('Listening for traffic on %s:%s on ip %s' % (group, port, interface)) while True: readable, _, _ = select.select([sock], [], [], .5) if readable: message, address = sock.recvfrom(65000) print('Received %r from %r' % (message, address)) finally: mcastsocket.leave_group( sock, group=group, iface=interface, )
def main(): # bind/listen on all interfaces, send with a TTL of 5 group, port = '224.1.1.2', 8053 interface = sys.argv[1] sock = mcastsocket.create_socket( ('', port), TTL=5, ) mcastsocket.join_group( sock, group=group, iface = interface, ) try: print('Listening for traffic on %s:%s on ip %s'%(group,port,interface)) while True: readable, _, _ = select.select([sock], [], [], .5) if readable: message, address = sock.recvfrom(65000) print('Received %r from %r' % (message, address)) finally: mcastsocket.leave_group( sock, group=group, iface = interface, )
def test_create_socket_v3(self): group = '224.1.1.2' sock = mcastsocket.create_socket( ('', 8000), TTL=5, ) try: ssm = '198.51.100.23' mcastsocket.join_group( sock, group=group, iface='127.0.0.1', # If commented out, we get a failure, # if present, we filter... yay ssm=ssm, ) try: self.send_to_group(group, b'moo') readable, writable, _ = select.select([sock], [], [], .5) if readable: (content, address) = sock.recvfrom(65000) assert not readable, ( 'Should not have received the message due to ssm filtering', address) finally: mcastsocket.leave_group( sock, group=group, iface='127.0.0.1', # If commented out, we get a failure, # if present, we filter... yay ssm=ssm, ) finally: sock.close()
def test_ipv6_basic(self): group = 'ff02::2' listen_addr = socket.getaddrinfo('::1', 8000, socket.AF_INET6, socket.SOCK_DGRAM)[0][-1] sock = mcastsocket.create_socket( # group binding does *not* work listen_addr, TTL=5, family=socket.AF_INET6, ) assert sock.family == socket.AF_INET6, sock.family mcastsocket.join_group( sock, group=group, iface='lo', ) self.send_to_group_v6(group, b'moo', family=socket.AF_INET6) readable, writable, _ = select.select([sock], [], [], .5) assert readable, 'Nothing received' (content, address) = sock.recvfrom(65000) assert content == b'moo', content assert address[1] == 8001, address assert address[0] == '::1', address mcastsocket.leave_group( sock, group=group, ) sock.close()
def test_ipv6_basic(self): group = 'ff02::2' listen_addr = socket.getaddrinfo( '::1', 8000, socket.AF_INET6, socket.SOCK_DGRAM )[0][-1] sock = mcastsocket.create_socket( # group binding does *not* work listen_addr, TTL=5, family=socket.AF_INET6, ) assert sock.family == socket.AF_INET6, sock.family mcastsocket.join_group( sock, group=group, iface = 'lo', ) self.send_to_group_v6(group, b'moo', family=socket.AF_INET6) readable, writable, _ = select.select([sock], [], [], .5) assert readable, 'Nothing received' (content, address) = sock.recvfrom(65000) assert content == b'moo', content assert address[1] == 8001, address assert address[0] == '::1', address mcastsocket.leave_group( sock, group=group, ) sock.close()
def sendToMulticast(): # bind/listen on all interfaces, send with a TTL of 5 group, port = 'ff02::1', 20002 interface = "wlan0" listen_addr = socket.getaddrinfo( '::', port, socket.AF_INET6, socket.SOCK_DGRAM )[0][-1] sock = mcastsocket.create_socket( listen_addr, TTL=2, family=socket.AF_INET6, loop=True, ) mcastsocket.join_group( sock, group=group, iface=interface, ) try: _, writable, _ = select.select([], [sock], [], .5) if writable: msg = { "requestTime": datetime.timestamp(datetime.now()) } sock.sendto(json.dumps(msg).encode() , (group, port)) finally: mcastsocket.leave_group( sock, group=group, iface=interface, )
def main(): # bind/listen on all interfaces, send with a TTL of 5 group, port = '224.1.1.2', 8053 interface = sys.argv[1] sock = mcastsocket.create_socket( ('', port), TTL=5, loop=True, ) mcastsocket.join_group( sock, group=group, iface=interface, ) try: print('Sending traffic on %s:%s %s' % (group, port, interface)) while True: _, writable, _ = select.select([], [sock], [], .5) if writable: sock.sendto(b'message', (group, port)) time.sleep(.5) finally: mcastsocket.leave_group( sock, group=group, iface=interface, )
def Revc(): # bind/listen on all interfaces, send with a TTL of 5 group, port = 'ff02::1', 20002 interface = "wlan0" listen_addr = socket.getaddrinfo('::', port, socket.AF_INET6, socket.SOCK_DGRAM)[0][-1] sock = mcastsocket.create_socket(listen_addr, TTL=1, family=socket.AF_INET6) mcastsocket.join_group( sock, group=group, iface=interface, ) try: while True: readable, _, _ = select.select([sock], [], [], .5) if readable: message, address = sock.recvfrom(65000) message = json.loads(message.decode()) if address[0] != local_link_ipv6: sockAddr = socket.getaddrinfo(address[0], 20003, family=socket.AF_INET6, proto=socket.IPPROTO_UDP) with socket.socket(family=socket.AF_INET6, type=socket.SOCK_DGRAM) as ss: msg = { "replyFrom": local_link_ipv6, "requestTime": message["requestTime"] } ss.sendto(json.dumps(msg).encode(), sockAddr[0][-1]) ss.close() finally: mcastsocket.leave_group( sock, group=group, iface=interface, )
def main(): # bind/listen on all interfaces, send with a TTL of 5 group, port = '224.1.1.2', 8000 sock = mcastsocket.create_socket( (group, port), TTL=5, ) mcastsocket.join_group( sock, group=group, #iface='local_ip_to_listen', ) sock.sendto(b'message', (group, port)) readable, [], [] = select.select([sock], [], [], .5) assert readable message, address = sock.recvfrom(65000) print('Received %r from %r' % (message, address)) mcastsocket.leave_group(sock, group=group)
def main(): # bind/listen on all interfaces, send with a TTL of 5 group, port = '224.1.1.2', 8000 sock = mcastsocket.create_socket( (group, port), TTL=5, ) mcastsocket.join_group( sock, group=group, #iface='local_ip_to_listen', ) sock.sendto(b'message', (group, port)) readable, [], [] = select.select([sock], [], [], .5) assert readable message, address = sock.recvfrom(65000) print('Received %r from %r' % (message, address)) mcastsocket.leave_group( sock, group=group )
def test_create_socket_v2(self): group = '224.1.1.2' sock = mcastsocket.create_socket( ('', 8000), TTL=5, ) mcastsocket.join_group( sock, group=group, iface='127.0.0.1', ) self.send_to_group(group, b'moo') readable, writable, _ = select.select([sock], [], [], .5) assert readable, 'Nothing received' (content, address) = sock.recvfrom(65000) assert content == b'moo', content assert address == ('127.0.0.1', 8001), address mcastsocket.leave_group( sock, group=group, iface='127.0.0.1', ) sock.close()