How to quiet SimpleHTTPServer?

You can subclass SimpleHTTPServer.SimpleHTTPRequestHandler and override the log_message method. Here is the method you will be overriding, sans docstring:

def log_message(self, format, *args):
    sys.stderr.write("%s - - [%s] %s\n" %
                     (self.address_string(),
                      self.log_date_time_string(),
                      format%args))

So to simply ignore all messages, replace the body of the function with pass. For more fine-grained control (i.e if you still want error messages printed), you may instead override the log_request and/or log_error methods. Original methods are like this:

def log_request(self, code='-', size='-'):
    self.log_message('"%s" %s %s',
                     self.requestline, str(code), str(size))

def log_error(self, format, *args):
    self.log_message(format, *args)

From 2.7 to 3.1 the module names change, but these methods are unchanged.


There is no need to subclass.

Easier solution:

http_handler = SimpleHTTPServer.SimpleHTTPRequestHandler
http_handler.log_message = lambda a, b, c, d, e: None

Run it this way in Bash:

python -m SimpleHTTPServer &>/dev/null