Commit 209c3adb authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

log sent and received messages

parent bfe46959
import argparse
import logging
import socket
import struct
import sys
if not hasattr(socket, 'SO_BINDTODEVICE'):
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.add_argument("multicast_group", help="Multicast IPv6 to use for subscription")
......@@ -13,19 +16,21 @@ args = parser.parse_args()
server_address = ('', 10000)
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(server_address)
# on all interfaces.
for ifidx, ifname in socket.socket.if_nameindex():
print("joining %s @ [%d]%s" % (args.multicast_group, ifidx, ifname))
for ifidx, ifname in socket.if_nameindex():
if ifname in ('lo', 'can0'):
continue
logging.info('%s joining %s @ [%d]%s', hostname, args.multicast_group, ifidx, ifname)
bifidx = struct.pack("@i", ifidx)
mreq = socket.inet_pton(socket.AF_INET6, args.multicast_group) + \
bifidx
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) # = IPV6_ADD_MEMBERSHIP
mreq = socket.inet_pton(socket.AF_INET6, args.multicast_group) + bifidx
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
try:
while True:
data, address = sock.recvfrom(10240)
print(data.decode("utf-8"))
logging.info('%s received %s', hostname, data.decode("utf-8"))
finally:
sock.close()
import argparse
import logging
import socket
import struct
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.add_argument("addr", help="IPv6 address to use for source")
parser.add_argument("multicast_group", help="Multicast IPv6 address to use for destination")
args = parser.parse_args()
print(args.addr)
print(args.multicast_group)
logging.info('host %s will send to %s with source address %s', hostname, args.multicast_group, args.addr)
ttl = 12
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((args.addr, 10000))
try:
......@@ -21,6 +24,7 @@ try:
input_msg = input('msg --> ')
try:
sock.sendto(input_msg.encode("utf-8"), (args.multicast_group, 10000))
logging.info('%s sent %s', hostname, input_msg)
except Exception:
traceback.print_exc()
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