Commit 4528f190 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

log sent and received messages

parent bfe46959
import argparse import argparse
import logging
import socket import socket
import struct import struct
import sys
if not hasattr(socket, 'SO_BINDTODEVICE'): if not hasattr(socket, 'SO_BINDTODEVICE'):
socket.SO_BINDTODEVICE = 25 socket.SO_BINDTODEVICE = 25
hostname = socket.gethostname()
logging.basicConfig(filename=hostname+'-client6.log', encoding='utf-8', format='%(asctime)s %(message)s', level=logging.DEBUG)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("multicast_group", help="Multicast IPv6 to use for subscription") parser.add_argument("multicast_group", help="Multicast IPv6 to use for subscription")
...@@ -16,16 +19,17 @@ sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) ...@@ -16,16 +19,17 @@ sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.bind(server_address) sock.bind(server_address)
# on all interfaces. # on all interfaces.
for ifidx, ifname in socket.socket.if_nameindex(): for ifidx, ifname in socket.if_nameindex():
print("joining %s @ [%d]%s" % (args.multicast_group, ifidx, ifname)) if ifname in ('lo', 'can0'):
continue
logging.info('%s joining %s @ [%d]%s', hostname, args.multicast_group, ifidx, ifname)
bifidx = struct.pack("@i", ifidx) bifidx = struct.pack("@i", ifidx)
mreq = socket.inet_pton(socket.AF_INET6, args.multicast_group) + \ mreq = socket.inet_pton(socket.AF_INET6, args.multicast_group) + bifidx
bifidx sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) # = IPV6_ADD_MEMBERSHIP
try: try:
while True: while True:
data, address = sock.recvfrom(10240) data, address = sock.recvfrom(10240)
print(data.decode("utf-8")) logging.info('%s received %s', hostname, data.decode("utf-8"))
finally: finally:
sock.close() sock.close()
import argparse import argparse
import logging
import socket import socket
import struct
import traceback import traceback
hostname = socket.gethostname()
logging.basicConfig(filename=hostname+'-server6.log', encoding='utf-8', format='%(asctime)s %(message)s', level=logging.DEBUG)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("addr", help="IPv6 address to use for source") parser.add_argument("addr", help="IPv6 address to use for source")
parser.add_argument("multicast_group", help="Multicast IPv6 address to use for destination") parser.add_argument("multicast_group", help="Multicast IPv6 address to use for destination")
args = parser.parse_args() args = parser.parse_args()
print(args.addr) logging.info('host %s will send to %s with source address %s', hostname, args.multicast_group, args.addr)
print(args.multicast_group)
ttl = 12 ttl = 12
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
...@@ -21,6 +23,7 @@ try: ...@@ -21,6 +23,7 @@ try:
input_msg = input('msg --> ') input_msg = input('msg --> ')
try: try:
sock.sendto(input_msg.encode("utf-8"), (args.multicast_group, 10000)) sock.sendto(input_msg.encode("utf-8"), (args.multicast_group, 10000))
logging.info('%s sent %s', hostname, input_msg)
except Exception: except Exception:
traceback.print_exc() traceback.print_exc()
continue continue
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment