python-3.xbasehttpserver

How to print an empty date_time_string in BaseHTTPRequestHandler?


I have built a tiny httpd using BaseHTTPRequestHandler in Python3. At the moment, it logs every HTTP GET request in systemd journal like this:

Oct 18 23:41:51 ubuntu httpd.py[19414]: 192.168.0.17 - - [18/Oct/2018 23:41:51] "GET / HTTP/1.1" 200 -

I would like to avoid printing the timestamp printed by my httpd. Handler for HTTP GET requests is following:

def do_GET(self):
  self.send_response(200)
  self.send_header('Content-type','text/html')
  self.date_time_string()
  self.end_headers()

Is it possible to disable printing the timestamp with date_time_string()? I tried with self.date_time_string(None), self.date_time_string(timestamp=None), but this didn't change anything.


Solution

  • To achieve desired log format, you need to write your own implementation of log_message method. You can go ahead and redefine log_error and log_request if you want to.