From fbd53b0a21154eaa04c228b4e713052c747a25bd Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Fri, 22 Jul 2022 13:41:46 -0400 Subject: [PATCH 1/4] Add property to get/set the request buffer size --- adafruit_httpserver.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/adafruit_httpserver.py b/adafruit_httpserver.py index 206375d..4fb4556 100644 --- a/adafruit_httpserver.py +++ b/adafruit_httpserver.py @@ -361,3 +361,25 @@ def poll(self): # connection reset by peer, try again later. return raise + + @property + def requestbuffersize(self) -> int: + """ + The maximum size of the incoming request buffer. If the default size isn't + adequate to handle your incoming data you can set this after creating the + server instance. + + Default size is 1024 bytes. + + Example:: + + server = HTTPServer(pool) + server.requestbuffersize = 2048 + + server.serve_forever(str(wifi.radio.ipv4_address)) + """ + return len(self._buffer) + + @requestbuffersize.setter + def requestbuffersize(self, value: int) -> None: + self._buffer = bytearray(value) From 44b9b013c4537a76b120dbb31158abfdeca5660f Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Fri, 22 Jul 2022 13:57:03 -0400 Subject: [PATCH 2/4] Update examples doc to include the temp and polling code. Minor fixes to documentation. --- adafruit_httpserver.py | 6 +++--- docs/examples.rst | 19 +++++++++++++++++++ examples/httpserver_simplepolling.py | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/adafruit_httpserver.py b/adafruit_httpserver.py index 4fb4556..c25e495 100644 --- a/adafruit_httpserver.py +++ b/adafruit_httpserver.py @@ -174,7 +174,7 @@ def mime_type(filename): class HTTPResponse: - """Details of an HTTP response. Use in @`HTTPServer.route` decorator functions.""" + """Details of an HTTP response. Use in `HTTPServer.route` decorator functions.""" _HEADERS_FORMAT = ( "HTTP/1.1 {}\r\n" @@ -365,8 +365,8 @@ def poll(self): @property def requestbuffersize(self) -> int: """ - The maximum size of the incoming request buffer. If the default size isn't - adequate to handle your incoming data you can set this after creating the + The maximum size of the incoming request buffer. If the default size isn't + adequate to handle your incoming data you can set this after creating the server instance. Default size is 1024 bytes. diff --git a/docs/examples.rst b/docs/examples.rst index 336dd2c..0100e37 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -6,3 +6,22 @@ Ensure your device works with this simple test. .. literalinclude:: ../examples/httpserver_simpletest.py :caption: examples/httpserver_simpletest.py :linenos: + +Temperature test +-------------------- + +Send the microcontroller temperature back to the browser with this simple test. + +.. literalinclude:: ../examples/httpserver_temperature.py + :caption: examples/httpserver_temperature.py + :linenos: + +Simple polling test +------------------- + +If you want your code to do more than just serve web pages, +use the start/poll methods as shown in this example. + +.. literalinclude:: ../examples/httpserver_simplepolling.py + :caption: examples/httpserver_simplepolling.py + :linenos: diff --git a/examples/httpserver_simplepolling.py b/examples/httpserver_simplepolling.py index 12211fc..1fe798f 100644 --- a/examples/httpserver_simplepolling.py +++ b/examples/httpserver_simplepolling.py @@ -30,6 +30,7 @@ def base(request): # pylint: disable=unused-argument while True: try: + # do something useful in this section # processing any waiting requests server.poll() except OSError: From f4e01bf7fb6995cd3ef497b03214d6716b3498be Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Sun, 24 Jul 2022 18:26:14 -0400 Subject: [PATCH 3/4] Use snake case for property. Add more details to example comment. --- adafruit_httpserver.py | 6 +++--- examples/httpserver_simplepolling.py | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/adafruit_httpserver.py b/adafruit_httpserver.py index c25e495..99b8d77 100644 --- a/adafruit_httpserver.py +++ b/adafruit_httpserver.py @@ -363,7 +363,7 @@ def poll(self): raise @property - def requestbuffersize(self) -> int: + def request_buffer_size(self) -> int: """ The maximum size of the incoming request buffer. If the default size isn't adequate to handle your incoming data you can set this after creating the @@ -380,6 +380,6 @@ def requestbuffersize(self) -> int: """ return len(self._buffer) - @requestbuffersize.setter - def requestbuffersize(self, value: int) -> None: + @request_buffer_size.setter + def request_buffer_size(self, value: int) -> None: self._buffer = bytearray(value) diff --git a/examples/httpserver_simplepolling.py b/examples/httpserver_simplepolling.py index 1fe798f..6bec7e8 100644 --- a/examples/httpserver_simplepolling.py +++ b/examples/httpserver_simplepolling.py @@ -30,8 +30,11 @@ def base(request): # pylint: disable=unused-argument while True: try: - # do something useful in this section - # processing any waiting requests + # do something useful in this section, + # for example read a sensor and capture an average, + # or a running total of the last 10 samples + + # process any waiting requests server.poll() except OSError: continue From 6528b6867d15d00adc4aab6c450c7f2768d2ec20 Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Mon, 25 Jul 2022 20:32:41 -0400 Subject: [PATCH 4/4] Update example in property comment --- adafruit_httpserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_httpserver.py b/adafruit_httpserver.py index 99b8d77..28dad98 100644 --- a/adafruit_httpserver.py +++ b/adafruit_httpserver.py @@ -374,7 +374,7 @@ def request_buffer_size(self) -> int: Example:: server = HTTPServer(pool) - server.requestbuffersize = 2048 + server.request_buffer_size = 2048 server.serve_forever(str(wifi.radio.ipv4_address)) """