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

3

75

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.

Theretofore answered 2/8, 2010 at 15:39 Comment(1)
The more time I use Python, the more I realise just how much gibberish is involved. Take this: ``` from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler class MyHanlder(BaseHTTPRequestHandler): def do_GET(self): ```Mccafferty
R
155

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()
Reconvert answered 2/8, 2010 at 16:3 Comment(6)
This gave me such a headache because my httpserver wasn't returning anything when I ran it in the background without a tty. You are a life saver. Thanks!Chrysa
@agressen, same frustration here -- and same thanks to MattH. My app is implemented as a .pyw because it's a Tkinter interface, so not having a console window was preventing any replies from being sent.Blackface
you probably want to override log_request instead of log_message, since the latter is used for other types of logging including errros.Osteo
Thanks for the hint, which leads me to this official docFrisbie
Note that if you still want to see errors (which you usually do), you can instead override the log_request function instead, as def log_request(self, code='-', size='-'): return.Kelbee
took me a while to notice the differenceFredrick
S
1

To silent/quiet HTTP Server and Basic HTTPRequestHandler stderr output, 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)
    )
Spotter answered 29/12, 2023 at 11:5 Comment(1)
This explains @matth's answer https://mcmap.net/q/268635/-how-to-silent-quiet-httpserver-and-basichttprequesthandler-39-s-stderr-output and extends it (to show what the original code does, which might be useful).Asthenopia
S
0

This answer might be late but you can also try changing/setting sys.stdout and sys.stderr

import io

stream = io.StringIO()
if (sys.stdout is None) or (sys.stderr is None):
    sys.stdout, sys.stderr = stream, stream

It worked for me.

Samaritan answered 29/12, 2023 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.