为什么我的 python HttpServer 在关闭时转储所有收到的请求?

Why does my python HttpServer dump all received requests on close?

我有以下请求处理程序:

class MyHandler(BaseHTTPRequestHandler):

    def do_POST(self) -> None:
        if self.path == "/myRoute":
            self.handle_route()

    def handle_route(self):
        print("Handling /myRoute")
        received_bytes = self.rfile.read(int(self.headers['Content-Length']))
        data = json.loads(received_bytes.decode('utf8').replace("'", '"'))
        logger.info(json.dumps(data, indent=4, sort_keys=True))
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(bytes("{\"Success\":true,\"Exception\":\"\"}", "utf-8"))

我正在成功处理 POST 到路线 /myRoute。但是,当我在以下用法后关闭时:

my_server = server.HTTPServer(("localhost", 8080), MyHandler)
my_thread = threading.Thread(target=my_server.serve_forever)
my_thread.daemon = True
my_thread.start()
# do some work
my_server.server_close()

我看到收到的所有 POST 的“转储”。这让我觉得我没有正确地将我的回复返回给客户:

127.0.0.1 - - [04/Aug/2020 12:01:58] "POST /myRoute HTTP/1.1" 200 -
127.0.0.1 - - [04/Aug/2020 12:02:35] "POST /myRoute HTTP/1.1" 200 -
127.0.0.1 - - [04/Aug/2020 12:03:11] "POST /myRoute HTTP/1.1" 200 -
.
.
.

让我觉得这个 甚至更多 的原因是它与我在执行应用程序期间收到的请求数量相同。

我是否正确地将我的回复发回给了客户?

为了解决这个问题,我简单地覆盖了 BaseHttpRequestHandler.log_message() 不做任何事情:

    def log_message(self, format: str, *args: Any) -> None:
        """
        Override of BaseHTTPRequestHandler to skip logging of messages.
        Parameters
        ----------
        format: str
            The str format.
        args: Any
            The values to log.
        """

然后我在路由处理方法中仅通过典型的logger = logging.getLogger(__name__)记录了我感兴趣的内容。