I have the following request handler:
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"))
I am successfully handling POST
s to the route /myRoute
. However, when I close after the following usage:
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()
I see a "dump" of all the POST
s received. This makes me think I am not properly flushing my response(s) back to the client:
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 -
.
.
.
What makes me think this even more is that it is the same number of requests I received during the execution of my application.
Am I properly sending my responses back to the client?
To fix this, I simply overrided the BaseHttpRequestHandler.log_message()
to do nothing:
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.
"""
Then I only logged what I was interested in via the typical logger = logging.getLogger(__name__)
in my route handling methods.