Changeset 323 for py-tcpmultiplexer


Ignore:
Timestamp:
Jul 19, 2011, 10:11:40 PM (13 years ago)
Author:
Rick van der Zwet
Message:

Get Socket Timeout to make sure to continue quickly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • py-tcpmultiplexer/TCPMultiplexer.py

    r322 r323  
    1212import argparse
    1313import logging
     14import os
     15import socket
    1416import sys
    1517import threading
    1618import time
    1719import urllib2
    18 import os
    1920
    2021# Some boring defaults
     
    2324DEFAULT_CONFIG = 'streams.yaml'
    2425DEFAULT_DOCUMENTROOT = './htdocs'
     26DEFAULT_TIMEOUT = 2
    2527
    2628
     
    3032  '/cam1/audio' : 'http://172.16.0.67:8080/audio.wav',
    3133  }
     34
    3235
    3336# Global variables used as ring-buffers or shared-storage
     
    9598          del urldata[urlid][self]
    9699
     100
     101
    97102def get_data(url, urlid, *args):
     103  """ Fetch the DATA from the WAV or MJPEG Stream """
    98104  global running, dataheader, urlheader, urldata
    99105  logger = logging.getLogger('recv_id=' + urlid)
     
    101107  logger.info("Starting thread '%s' (%s)", url, urlid)
    102108  while running:
     109    urlheader[urlid] = None
     110    dataheader[urlid] = None
     111    urldata[urlid] = None
    103112    try:
    104113      f = urllib2.urlopen(url)
    105114      urlheader[urlid] = ''.join(f.info().headers)
    106115      urldata[urlid] = dict()
     116
     117      # Find datatype
    107118      datatype = None
    108119      urltype = f.info().gettype()
     
    112123      elif urltype == 'multipart/x-mixed-replace':
    113124        datatype = 'mjpeg'
     125
     126      # Be verbose to the user
     127      if datatype:
     128        logger.info("'%s' Identified as %s", url, datatype)
    114129      else:
    115130        logger.warning("Cannot find file type of '%s'", url)
    116131
    117       if datatype:
    118         logger.info("'%s' Identified as %s", url, datatype)
    119 
     132      # Set the correct data header
    120133      if datatype == 'wav':
    121134        # WAV header
     
    147160      while running:
    148161        data = f.read(10000)
     162        # logger.debug("Received data chunk with length: %s", len(data))
    149163        for key in urldata[urlid].keys():
    150164          urldata[urlid][key].append(data)
     
    164178  parser.add_argument('--host', dest='host', default=DEFAULT_HOST, help='Listen to IP [default: %s]' % DEFAULT_HOST)
    165179  parser.add_argument('--port', dest='port', default=DEFAULT_PORT, type=int, help='Listen to PORT [default: %s]' % DEFAULT_PORT)
     180  parser.add_argument('--timeout', dest='timeout', default=DEFAULT_TIMEOUT, type=int, help='Default socket timeout [default: %s]' % DEFAULT_TIMEOUT)
    166181  parser.add_argument('--stream-cfg', dest='stream_cfg', default=DEFAULT_CONFIG, help='YAML Stream configuration [default: %s]' % DEFAULT_CONFIG)
    167182  parser.add_argument('--document-root', dest='document_root', default=DEFAULT_DOCUMENTROOT, help='Document Root File Directory [default: %s]' % DEFAULT_DOCUMENTROOT)
     
    170185  document_root = os.path.abspath(os.path.join(os.path.dirname(__file__),args.document_root))
    171186  logging.info("Serving '/htdocs' from document_root '%s'", document_root)
     187
     188  # Set the timeout
     189  socket.setdefaulttimeout(args.timeout)
    172190
    173191  # Inport streams
Note: See TracChangeset for help on using the changeset viewer.