How to silent/quiet HTTPServer and BasicHTTPRequestHandler's stderr output?

Hai Vu picture Hai Vu · Aug 2, 2010 · Viewed 21.5k times · Source

I am writing a simple http server as part of my project. Below is a skeleton of my script:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyHanlder(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('<html><body><p>OK</p></body></html>')

httpd = HTTPServer(('', 8001), MyHanlder)
httpd.serve_forever()

My question: how do I suppress the stderr log output my script produces every time a client connects to my server?

I have looked at the HTTPServer class up to its parent, but was unable to find any flag or function call to achieve this. I also looked at the BaseHTTPRequestHandler class, but could not find a clue. I am sure there must be a way. If you do, please share with me and others; I appreciate your effort.

Answer

MattH picture MattH · Aug 2, 2010

This will probably do it:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('<html><body><p>OK</p></body></html>')
    def log_message(self, format, *args):
        return

httpd = HTTPServer(('', 8001), MyHandler)
httpd.serve_forever()