From 3aef11387fb8ba7efef2ed18706c3121b94b23a6 Mon Sep 17 00:00:00 2001 From: JR Rickerson Date: Tue, 25 Apr 2023 13:25:42 -0400 Subject: [PATCH 1/5] Add a bare minimum simpletest example --- examples/httpserver_simpletest.py | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/httpserver_simpletest.py diff --git a/examples/httpserver_simpletest.py b/examples/httpserver_simpletest.py new file mode 100644 index 0000000..ccb32a3 --- /dev/null +++ b/examples/httpserver_simpletest.py @@ -0,0 +1,38 @@ +# SPDX-FileCopyrightText: 2023 JR Rickerson for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense + +import os + +import socketpool +import wifi + +from adafruit_httpserver.mime_type import MIMEType +from adafruit_httpserver.request import HTTPRequest +from adafruit_httpserver.response import HTTPResponse +from adafruit_httpserver.server import HTTPServer + + +ssid = os.getenv("WIFI_SSID") +password = os.getenv("WIFI_PASSWORD") + +print("Connecting to", ssid) +wifi.radio.connect(ssid, password) +print("Connected to", ssid) + +pool = socketpool.SocketPool(wifi.radio) +server = HTTPServer(pool, "") + + +@server.route("/") +def base(request: HTTPRequest): + """ + Serve a static text response to ensure the server is running. + """ + with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response: + message = "Hello from the CircuitPython HTTPServer!" + response.send(message) + + +print(f"Listening on http://{wifi.radio.ipv4_address}:80") +server.serve_forever(str(wifi.radio.ipv4_address)) From eb89ecf8a5a013ecf0ea5675eb35a10f4d194006 Mon Sep 17 00:00:00 2001 From: JR Rickerson Date: Tue, 25 Apr 2023 16:14:26 -0400 Subject: [PATCH 2/5] Merge the simpletest example into simple_serve --- examples/httpserver_simple_serve.py | 9 ++++--- examples/httpserver_simpletest.py | 38 ----------------------------- 2 files changed, 5 insertions(+), 42 deletions(-) delete mode 100644 examples/httpserver_simpletest.py diff --git a/examples/httpserver_simple_serve.py b/examples/httpserver_simple_serve.py index 226d8f2..b556d27 100644 --- a/examples/httpserver_simple_serve.py +++ b/examples/httpserver_simple_serve.py @@ -21,16 +21,17 @@ print("Connected to", ssid) pool = socketpool.SocketPool(wifi.radio) -server = HTTPServer(pool, "/static") +server = HTTPServer(pool, "") @server.route("/") def base(request: HTTPRequest): """ - Serve the default index.html file. + Serve a default static plain text message. """ - with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response: - response.send_file("index.html") + with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response: + message = "Hello from the CircuitPython HTTPServer!" + response.send(message) print(f"Listening on http://{wifi.radio.ipv4_address}:80") diff --git a/examples/httpserver_simpletest.py b/examples/httpserver_simpletest.py deleted file mode 100644 index ccb32a3..0000000 --- a/examples/httpserver_simpletest.py +++ /dev/null @@ -1,38 +0,0 @@ -# SPDX-FileCopyrightText: 2023 JR Rickerson for Adafruit Industries -# -# SPDX-License-Identifier: Unlicense - -import os - -import socketpool -import wifi - -from adafruit_httpserver.mime_type import MIMEType -from adafruit_httpserver.request import HTTPRequest -from adafruit_httpserver.response import HTTPResponse -from adafruit_httpserver.server import HTTPServer - - -ssid = os.getenv("WIFI_SSID") -password = os.getenv("WIFI_PASSWORD") - -print("Connecting to", ssid) -wifi.radio.connect(ssid, password) -print("Connected to", ssid) - -pool = socketpool.SocketPool(wifi.radio) -server = HTTPServer(pool, "") - - -@server.route("/") -def base(request: HTTPRequest): - """ - Serve a static text response to ensure the server is running. - """ - with HTTPResponse(request, content_type=MIMEType.TYPE_TXT) as response: - message = "Hello from the CircuitPython HTTPServer!" - response.send(message) - - -print(f"Listening on http://{wifi.radio.ipv4_address}:80") -server.serve_forever(str(wifi.radio.ipv4_address)) From c87d7dd2a522c7dcd30cb259bec03332e09d4b2b Mon Sep 17 00:00:00 2001 From: JR Rickerson Date: Tue, 25 Apr 2023 16:15:33 -0400 Subject: [PATCH 3/5] Rename simple_serve example for consistency. Many of the other CircuitPython libraries use the "simpletest" nomenclature for their examples - rename the simplest example to match the convention. --- examples/{httpserver_simple_serve.py => httpserver_simpletest.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{httpserver_simple_serve.py => httpserver_simpletest.py} (100%) diff --git a/examples/httpserver_simple_serve.py b/examples/httpserver_simpletest.py similarity index 100% rename from examples/httpserver_simple_serve.py rename to examples/httpserver_simpletest.py From 0fac5799bd8ae89014f1445f2f88555fc0bf9cc0 Mon Sep 17 00:00:00 2001 From: JR Rickerson Date: Tue, 25 Apr 2023 16:23:37 -0400 Subject: [PATCH 4/5] Update the docs for the renamed example --- docs/examples.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index befd552..433b66c 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -1,10 +1,10 @@ -Simple file serving +Simple Test ------------------- -Serving the content of index.html from the filesystem. +Serving a simple static text message. -.. literalinclude:: ../examples/httpserver_simple_serve.py - :caption: examples/httpserver_simple_serve.py +.. literalinclude:: ../examples/httpserver_simpletest.py + :caption: examples/httpserver_simpletest.py :linenos: If you want your code to do more than just serve web pages, From 6e1926a84ac4868b0b6acea86aa8be5a206ae657 Mon Sep 17 00:00:00 2001 From: JR Rickerson Date: Tue, 25 Apr 2023 17:05:43 -0400 Subject: [PATCH 5/5] Use "/static" root path to avoid security concerns --- examples/httpserver_simpletest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/httpserver_simpletest.py b/examples/httpserver_simpletest.py index b556d27..c04ce2d 100644 --- a/examples/httpserver_simpletest.py +++ b/examples/httpserver_simpletest.py @@ -21,7 +21,7 @@ print("Connected to", ssid) pool = socketpool.SocketPool(wifi.radio) -server = HTTPServer(pool, "") +server = HTTPServer(pool, "/static") @server.route("/")