Changeset 313 for py-tcpmultiplexer


Ignore:
Timestamp:
Jul 19, 2011, 1:08:43 PM (13 years ago)
Author:
Rick van der Zwet
Message:

Use argparse for argument parsing instead of some random hacks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • py-tcpmultiplexer/TCPMultiplexer.py

    r312 r313  
    11#!/usr/bin/env python
    2 #
     2"""
    33# Multiplexer for HTTP streams, used for broadcast of MPJEG and WAV streams
    44#
     
    66#
    77# Rick van der Zwet <info@rickvanderzwet.nl>
    8 #
     8"""
     9
    910import SocketServer
    1011import argparse
     
    1718# Some boring defaults
    1819DEFAULT_HOST = '0.0.0.0'
    19 DEFAULT_PORT = '9999'
     20DEFAULT_PORT = 9999
     21DEFAULT_CONFIG = 'streams.yaml'
     22
     23# URL : TARGET
     24DEFAULT_STREAMS = {
     25  '/cam1/video' : 'http://172.16.0.67:8080/videofeed',
     26  '/cam1/audio' : 'http://172.16.0.67:8080/audio.wav',
     27  }
    2028
    2129# Global variables used as ring-buffers or shared-storage
     
    117125
    118126if __name__ == "__main__":
    119   parser = argparse.ArgumentParser(description='Process some integers.')
     127  parser = argparse.ArgumentParser(description=__doc__,formatter_class=argparse.RawDescriptionHelpFormatter)
     128  parser.add_argument('--host', dest='host', default=DEFAULT_HOST, help='Listen to IP [default: %s]' % DEFAULT_HOST)
     129  parser.add_argument('--port', dest='port', default=DEFAULT_PORT, type=int, help='Listen to PORT [default: %s]' % DEFAULT_PORT)
     130  parser.add_argument('--stream-cfg', dest='stream_cfg', default=DEFAULT_CONFIG, help='YAML Stream configuration [default: %s]' % DEFAULT_CONFIG)
     131  args = parser.parse_args()
    120132
    121   # URL : TARGET
    122   DEFAULT_STREAMS = {
    123     '/cam1/video' : 'http://172.16.0.67:8080/videofeed',
    124     '/cam1/audio' : 'http://172.16.0.67:8080/audio.wav',
    125     }
    126   # Flexible enough to specify using yaml file (sys.argv) as well
    127   if len(sys.argv) > 1:
    128     config_file = sys.argv[1]
    129     try:
    130       import yaml
    131       streams = yaml.load(open(config_file))
    132     except (ImportError, IOError) as e:
    133       logging.error("Config file '%s' not readable or parsable (%s)", config_file, e)
     133  # Inport streams
     134  streams = DEFAULT_STREAMS
     135  try:
     136    import yaml
     137    streams = yaml.load(open(args.stream_cfg))
     138  except (ImportError, IOError) as e:
     139    logging.warning("Stream config file '%s' not readable or parsable (%s)", args.stream_cfg, e)
    134140
    135141
    136   HOST, PORT = "0.0.0.0", 9999
    137   # Create the server
     142  # Create the base server
    138143  try:
    139144    while True:
    140145      try:
    141146        ThreadedTCPServer.allow_reuse_address = True
    142         server = ThreadedTCPServer((HOST, PORT), MyTCPHandler)
     147        server = ThreadedTCPServer((args.host, args.port), MyTCPHandler)
    143148        break
    144149      except IOError, e:
    145         logging.warning('For conection %s:%s to become available (%s)', HOST, PORT, e)
     150        logging.warning('For conection %s:%s to become available (%s)', args.host, args.port , e)
    146151        time.sleep(1)
    147152  except KeyboardInterrupt:
     
    156161  # interrupt the program with Ctrl-C
    157162  try:
    158     logging.info('Serving at %s:%s', HOST,PORT)
     163    logging.info('Serving at %s:%s', args.host, args.port)
    159164    server.serve_forever()
    160165  except KeyboardInterrupt, IOError:
Note: See TracChangeset for help on using the changeset viewer.