#!/usr/bin/python3 import argparse import logging import sys from xmlrpc.client import ServerProxy DEFAULT_PORT = "8231" logging.basicConfig(level=logging.INFO, format='%(levelname)s %(message)s') LOG = logging.getLogger("att_ctrl_client") def parse_args(): """Command line argument parser""" parser = argparse.ArgumentParser() parser.add_argument("host", help="Hostname or ip address the server is \ serving on. (e.g. 'attctrl:8231 or 192.168.1.16'") parser.add_argument('attenuator', help="Attenuator to be set [0..7]") parser.add_argument('attenuation', help="Attenuation to be set [0.0 .. 31.5]") return parser.parse_args() def host_port(data): '''Determine hostname/address and port. Args: data (str): Hostname/address with optional port (e.g. 'localhost:1234' or '192.168.0.1'). Returns: str: Hostname/address str: Port ''' tmp = data.split(":") port = DEFAULT_PORT host = tmp[0] if len(tmp) > 1: port = tmp[1] return host, port def main(): args = parse_args() host, port = host_port(args.host) client = ServerProxy('http://{}:{}'.format(host, port)) try: dev = int(args.attenuator) except ValueError: LOG.error("attenuator needs to be convertible into int.") return 1 try: val = float(args.attenuation) except ValueError: LOG.error("attenuation needs to be convertible into float.") return 1 ret = client.set_attenuation(dev, val) LOG.info("xmlrpcclient: set_attenuation result: {}.".format(ret)) return ret if __name__ == "__main__": sys.exit(main())