Changeset 331
- Timestamp:
- Jul 20, 2011, 9:46:04 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
py-tcpmultiplexer/TCPMultiplexer.py
r325 r331 12 12 import argparse 13 13 import logging 14 import logging.handlers 14 15 import os 15 16 import socket … … 25 26 DEFAULT_DOCUMENTROOT = './htdocs' 26 27 DEFAULT_TIMEOUT = None 28 DEFAULT_LOGFILE = 'py-tcpmultiplexer.log' 27 29 28 30 … … 44 46 recv_timeout = None 45 47 46 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') 48 logger = logging.getLogger(__name__) 49 logger.setLevel(logging.INFO) 50 formatter = logging.Formatter('%(asctime)s - %(threadName)s - %(levelname)s - %(message)s') 51 52 ch = logging.StreamHandler() 53 ch.setFormatter(formatter) 54 55 logger.addHandler(ch) 47 56 48 57 class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): … … 106 115 """ Fetch the DATA from the WAV or MJPEG Stream """ 107 116 global running, dataheader, urlheader, urldata, stream_running, recv_timeout 108 logger = logging.getLogger('recv_id=' + urlid)109 117 # Fill buffers if needed 110 118 logger.info("Starting thread '%s' (%s)", url, urlid) … … 178 186 for key in urldata[urlid].keys(): 179 187 urldata[urlid][key].append(data) 180 except IOError,e:188 except (urllib2.URLError, IOError) as e: 181 189 #Enforce a connection reset 182 190 logger.warning("URL reset '%s' (%s)", url, e) … … 191 199 parser.add_argument('--host', dest='host', default=DEFAULT_HOST, help='Listen to IP [default: %s]' % DEFAULT_HOST) 192 200 parser.add_argument('--port', dest='port', default=DEFAULT_PORT, type=int, help='Listen to PORT [default: %s]' % DEFAULT_PORT) 201 parser.add_argument('--logfile', dest='logfile', default=DEFAULT_LOGFILE, help='File to write logfiles to [default: %s]' % DEFAULT_LOGFILE) 193 202 parser.add_argument('--timeout', dest='timeout', default=DEFAULT_TIMEOUT, type=int, help='Default socket timeout [default: %s]' % DEFAULT_TIMEOUT) 194 203 parser.add_argument('--stream-cfg', dest='stream_cfg', default=DEFAULT_CONFIG, help='YAML Stream configuration [default: %s]' % DEFAULT_CONFIG) … … 196 205 args = parser.parse_args() 197 206 207 # Add file logger 208 ch = logging.handlers.WatchedFileHandler(args.logfile) 209 ch.setFormatter(formatter) 210 logger.addHandler(ch) 211 198 212 document_root = os.path.abspath(os.path.join(os.path.dirname(__file__),args.document_root)) 199 logg ing.info("Serving '/htdocs' from document_root '%s'", document_root)213 logger.info("Serving '/htdocs' from document_root '%s'", document_root) 200 214 201 215 # Set the timeout 202 logg ing.info("Changing socket timeout from '%s' to '%s'", socket.getdefaulttimeout(), args.timeout)216 logger.info("Changing socket timeout from '%s' to '%s'", socket.getdefaulttimeout(), args.timeout) 203 217 recv_timeout = args.timeout 218 204 219 205 220 # Inport streams … … 209 224 streams.update(yaml.load(open(args.stream_cfg))) 210 225 except (ImportError, IOError) as e: 211 logg ing.warning("Stream config file '%s' not readable or parsable (%s)", args.stream_cfg, e)226 logger.warning("Stream config file '%s' not readable or parsable (%s)", args.stream_cfg, e) 212 227 213 228 … … 220 235 break 221 236 except IOError, e: 222 logg ing.warning('For conection %s:%s to become available (%s)', args.host, args.port , e)237 logger.warning('For conection %s:%s to become available (%s)', args.host, args.port , e) 223 238 time.sleep(1) 224 239 except KeyboardInterrupt: … … 226 241 227 242 for urlid, url in streams.iteritems(): 228 recv_threads.append(threading.Thread( target=get_data, args=(url,urlid)))243 recv_threads.append(threading.Thread(name=urlid, target=get_data, args=(url,urlid))) 229 244 recv_threads[-1].setDaemon(True) 230 245 recv_threads[-1].start() … … 233 248 # interrupt the program with Ctrl-C 234 249 try: 235 logg ing.info('Serving at %s:%s', args.host, args.port)250 logger.info('Serving at %s:%s', args.host, args.port) 236 251 server.serve_forever() 237 252 except KeyboardInterrupt, IOError: 238 logg ing.info('Shutting down, please wait...')253 logger.info('Shutting down, please wait...') 239 254 running = False 240 255 server.shutdown() 241 256 [thread.join() for thread in recv_threads] 242 logg ing.info('All done, good bye!')243 257 logger.info('All done, good bye!') 258
Note:
See TracChangeset
for help on using the changeset viewer.