From 70519875ee76bd8ab6ae49422ebc36598da28ec3 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Wed, 3 Jul 2019 12:40:00 +0200 Subject: Add stats sender to UNIX datagram socket --- example_stats_receiver.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 example_stats_receiver.py (limited to 'example_stats_receiver.py') diff --git a/example_stats_receiver.py b/example_stats_receiver.py new file mode 100755 index 0000000..99ce199 --- /dev/null +++ b/example_stats_receiver.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import logging +logging.basicConfig(level=logging.DEBUG) +import sys +import os +import os.path +import socket +import argparse +import yaml + +parser = argparse.ArgumentParser( + description="Example Stats UNIX Datagram Socket Receiver") +parser.add_argument('-s', '--socket', default="/tmp/stats", type=str, + help='Full path of the socket', + required=False) + +cli_args = parser.parse_args() + +sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + +if os.path.exists(cli_args.socket): + try: + os.unlink(cli_args.socket) + except OSError: + logging.warning("Could not unlink socket %s", cli_args.socket) + +sock.bind(cli_args.socket) + +logging.info("Starting receiver using socket '{}'".format(cli_args.socket)) + + +while True: + data, addr = sock.recvfrom(256) + + logging.info("RX from {}". format(addr)) + data = yaml.load(data) + print(data) -- cgit v1.2.3