Skip to content

Commit e671f8e

Browse files
committed
Added elapsed time to _debug_response_sent
1 parent 4ec5f9a commit e671f8e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

adafruit_httpserver/server.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
pass
1616

1717
from errno import EAGAIN, ECONNRESET, ETIMEDOUT
18+
from time import monotonic
1819
from traceback import print_exception
1920

2021
from .authentication import Basic, Token, Bearer, require_authentication
@@ -358,6 +359,8 @@ def poll(self) -> str:
358359
conn, client_address = self._sock.accept()
359360
conn.settimeout(self._timeout)
360361

362+
_debug_start_time = monotonic()
363+
361364
# Receive the whole request
362365
if (request := self._receive_request(conn, client_address)) is None:
363366
conn.close()
@@ -378,8 +381,10 @@ def poll(self) -> str:
378381
# Send the response
379382
response._send() # pylint: disable=protected-access
380383

384+
_debug_end_time = monotonic()
385+
381386
if self.debug:
382-
_debug_response_sent(response)
387+
_debug_response_sent(response, _debug_end_time - _debug_start_time)
383388

384389
return REQUEST_HANDLED_RESPONSE_SENT
385390

@@ -496,7 +501,7 @@ def _debug_started_server(server: "Server"):
496501
print(f"Started development server on http://{host}:{port}")
497502

498503

499-
def _debug_response_sent(response: "Response"):
504+
def _debug_response_sent(response: "Response", time_elapsed: float):
500505
"""Prints a message when after a response is sent."""
501506
# pylint: disable=protected-access
502507
client_ip = response._request.client_address[0]
@@ -505,8 +510,11 @@ def _debug_response_sent(response: "Response"):
505510
req_size = len(response._request.raw_request)
506511
status = response._status
507512
res_size = response._size
513+
time_elapsed_ms = f"{round(time_elapsed*1000)}ms"
508514

509-
print(f'{client_ip} -- "{method} {path}" {req_size} -- "{status}" {res_size}')
515+
print(
516+
f'{client_ip} -- "{method} {path}" {req_size} -- "{status}" {res_size} -- {time_elapsed_ms}'
517+
)
510518

511519

512520
def _debug_stopped_server(server: "Server"): # pylint: disable=unused-argument

docs/examples.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,19 @@ occurs during handling of the request in ``.serve_forever()``.
357357
This is how the logs might look like when debug mode is enabled::
358358

359359
Started development server on http://192.168.0.100:80
360-
192.168.0.101 -- "GET /" 194 -- "200 OK" 154
361-
192.168.0.101 -- "GET /example" 134 -- "404 Not Found" 172
362-
192.168.0.102 -- "POST /api" 1241 -- "401 Unauthorized" 95
360+
192.168.0.101 -- "GET /" 194 -- "200 OK" 154 -- 96ms
361+
192.168.0.101 -- "GET /example" 134 -- "404 Not Found" 172 -- 123ms
362+
192.168.0.102 -- "POST /api" 1241 -- "401 Unauthorized" 95 -- 64ms
363363
Traceback (most recent call last):
364364
...
365365
File "code.py", line 55, in example_handler
366366
KeyError: non_existent_key
367-
192.168.0.103 -- "GET /index.html" 242 -- "200 OK" 154
367+
192.168.0.103 -- "GET /index.html" 242 -- "200 OK" 154 -- 182ms
368368
Stopped development server
369369

370370
This is the default format of the logs::
371371

372-
{client_ip} -- "{request_method} {path}" {request_size} -- "{response_status}" {response_size}
372+
{client_ip} -- "{request_method} {path}" {request_size} -- "{response_status}" {response_size} -- {elapsed_ms}
373373

374374
If you need more information about the server or request, or you want it in a different format you can modify
375375
functions at the bottom of ``adafruit_httpserver/server.py`` that start with ``_debug_...``.

0 commit comments

Comments
 (0)