aboutsummaryrefslogtreecommitdiffstats
path: root/example_stats_receiver.py
blob: 99ce1990382700d4db9e993acfdb2a68524854d0 (plain)
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
32
33
34
35
36
37
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)