#!/usr/bin/env python # # Copyright 2009 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, # provided that the above copyright notice appear in all copies and that # both that copyright notice and this permission notice appear in # supporting documentation, and that the name of Vinay Sajip # not be used in advertising or publicity pertaining to distribution # of the software without specific, written prior permission. # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # from select import select import socket import sys import time import logging from logging import DEBUG, StreamHandler, Formatter, getLogger, debug, handlers from logging.handlers import TimedRotatingFileHandler, DEFAULT_HTTP_LOGGING_PORT class TimeSimulator: def __init__(self): self.sim_time = None self.start = 0 def time(self): if self.sim_time is None: return time.time() else: return time.time() - self.start + self.sim_time def localtime(self, t): return time.localtime(t) def gmtime(self, t): return time.gmtime(t) def strftime(self, fmt, v): return time.strftime(fmt, v) def set_sim_time(self, t): self.sim_time = t if t is None: self.start = 0 else: self.start = time.time() simulator = TimeSimulator() handlers.time = simulator logging.time = simulator trh = None # # HTTP receiver # from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler class LogRecordHTTPHandler(BaseHTTPRequestHandler): def do_GET(self): """Serve a GET request.""" self.send_head() s = "OK " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(simulator.time())) debug(s) self.wfile.write(s) if self.path == "/s": simulator.set_sim_time(trh.rolloverAt) elif self.path == "/r": simulator.set_sim_time(None) def do_HEAD(self): """Serve a HEAD request.""" self.send_head() def send_head(self): """Common code for GET and HEAD commands. This sends the response code and MIME headers. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do. """ self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() def log_message(self, *args): #comment out the following line if you don't want to show requests #apply(BaseHTTPRequestHandler.log_message, (self,) + args) pass class LogRecordHTTPReceiver(HTTPServer): def __init__(self, host='localhost', port=DEFAULT_HTTP_LOGGING_PORT, handler=LogRecordHTTPHandler): HTTPServer.__init__(self, (host, port), handler) self.abort = 0 self.timeout = 1 self.logname = None def serve_until_stopped(self): abort = 0 while not abort: rd, wr, ex = select([self.socket.fileno()], [], [], self.timeout) if rd: self.handle_request() abort = self.abort def run_http(httpserver=None): if not httpserver: httpserver = LogRecordHTTPReceiver() print "About to start HTTP server..." httpserver.serve_until_stopped() FORMAT_STR = "%(asctime)s pid: %(process)d %(levelname)-5s - %(message)s" if __name__ == "__main__": trh = TimedRotatingFileHandler("httpd.log", "midnight", 1) h2 = StreamHandler() f = Formatter(FORMAT_STR) trh.setFormatter(f) h2.setFormatter(f) r = getLogger('') r.addHandler(trh) r.addHandler(h2) r.setLevel(DEBUG) run_http()