Python отключение логов в http.server
При запуске сервера через http.server при каждом обращении к серверу пишется ip-адрес и время запроса. Можно ли отключить отображение этих логов в консоли?
from http.server import HTTPServer, BaseHTTPRequestHandler
class http_requests(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/image.png':
self.send_response(200)
self.send_header('Content-type', 'image/png')
self.end_headers()
with open('image.png', 'rb') as file:
self.wfile.write(file.read())
file.close()
httpd = HTTPServer(('localhost', 443), http_requests)
httpd.serve_forever()
Ответы (2 шт):
log_message(format, ...)Logs an arbitrary message tosys.stderr. This is typically overridden to create custom error logging mechanisms. The format argument is a standardprintf-style format string, where the additional arguments tolog_message()are applied as inputs to the formatting. The client ip address and current date and time are prefixed to every message logged.
Переопредели этот метод (или соседний чуть выше) в обработчике если запускаешь программно.
Если запускаешь как модуль, то можно просто заглушить вывод в /dev/null.
добавь у себя в хандлер перед всеми do_***(GET,POST)
def log_message(*args):
pass