Thriskel
Thriskel

Reputation: 431

How do I avoid the console logging of http.server

I have an http.server running inside my code to load a local html, but I don't want the server to log on console every time a request is made.

I looked into this post: How to silent/quiet HTTPServer and BasicHTTPRequestHandler's stderr output?

And tried it out but it returns an error: missing 3 required positional arguments

class main()
    def openHttpServer(self):
        port = 8000
        Handler = http.server.SimpleHTTPRequestHandler
        self.httpd = socketserver.TCPServer(("", port), Handler)
        self.httpd.serve_forever()

I expect it to work the same but without the SimpleHTTPRequestHandler logging in console.

Upvotes: 8

Views: 6081

Answers (1)

Thriskel
Thriskel

Reputation: 431

I fixed it by sub classing as suggested in several post

So, if I was using http.server.SimpleHTTPRequestHandler as Handler I now pass it as a subclass here:

class quietServer(http.server.SimpleHTTPRequestHandler):

And just write the function for the log_message with pass so it doesn't return a log on requests.

def log_message(self, format, *args):
        pass

So the whole code would be like this:

import http.server
import socketserver

PORT = 8080

class quietServer(http.server.SimpleHTTPRequestHandler):
    def log_message(self, format, *args):
        pass

with socketserver.TCPServer(("", PORT), quietServer) as httpd:
    httpd.serve_forever()

Upvotes: 15

Related Questions