Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 555
Thriskel Avatar asked May 14 '26 15:05

Thriskel


1 Answers

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()
like image 153
Thriskel Avatar answered May 16 '26 06:05

Thriskel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!