I’m playing with wsgiref.simple_server
to study the world of web servers.
I would like to control the log generated, but could not find anything about it in Python’s documentation.
My code looks like this:
JavaScript
x
9
1
from wsgiref.simple_server import make_server
2
3
def application(environ, start_response):
4
start_response('200 OK', headers)
5
return ['Hello World']
6
7
httpd = make_server('', 8000, application)
8
httpd.serve_forever()
9
Advertisement
Answer
wsgiref.simple_server.make_server
by default creates a WSGIServer
with WSGIRequestHandler
:
JavaScript
1
7
1
def make_server(
2
host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler):
3
"""Create a new WSGI server listening on `host` and `port` for `app`"""
4
server = server_class((host, port), handler_class)
5
server.set_app(app)
6
return server
7
WSGIRequestHandler
here extends from BaseHTTPServer.BaseHTTPRequestHandler
, where the logging magic turns out to be:
JavaScript
1
6
1
def log_message(self, format, *args):
2
sys.stderr.write("%s - - [%s] %sn" %
3
(self.client_address[0],
4
self.log_date_time_string(),
5
format%args))
6
So it’s logging to stderr, actually, not to python logging module. You can override this in your own handler:
JavaScript
1
5
1
class NoLoggingWSGIRequestHandler(WSGIRequestHandler):
2
3
def log_message(self, format, *args):
4
pass
5
And pass your custom handler to the server instead:
JavaScript
1
2
1
httpd = make_server('', 8000, application, handler_class=NoLoggingWSGIRequestHandler)
2