1
1
Simple Test
2
- -------------------
2
+ -----------
3
+
4
+ This is the minimal example of using the library.
5
+ This example is serving a simple static text message.
6
+
7
+ It also manually connects to the WiFi network.
8
+
9
+ .. literalinclude :: ../examples/httpserver_simpletest_manual.py
10
+ :caption: examples/httpserver_simpletest_manual.py
11
+ :linenos:
12
+
13
+ Although there is nothing wrong with this approach, from the version 8.0.0 of CircuitPython,
14
+ `it is possible to use the environment variables <https://docs.circuitpython.org/en/latest/docs/environment.html#circuitpython-behavior >`_
15
+ defined in ``settings.toml `` file to store secrets and configure the WiFi network.
16
+
17
+ This is the same example as above, but it uses the ``settings.toml `` file to configure the WiFi network.
18
+
19
+ **From now on, all the examples will use the ** ``settings.toml `` **file to configure the WiFi network. **
20
+
21
+ .. literalinclude :: ../examples/settings.toml
22
+ :caption: settings.toml
23
+ :linenos:
3
24
4
- Serving a simple static text message .
25
+ Note that we still need to import `` socketpool `` and `` wifi `` modules .
5
26
6
- .. literalinclude :: ../examples/httpserver_simpletest .py
7
- :caption: examples/httpserver_simpletest .py
27
+ .. literalinclude :: ../examples/httpserver_simpletest_auto .py
28
+ :caption: examples/httpserver_simpletest_auto .py
8
29
:linenos:
9
30
31
+ Serving static files
32
+ --------------------
33
+
34
+ It is possible to serve static files from the filesystem.
35
+ In this example we are serving files from the ``/static `` directory.
36
+
37
+ In order to save memory, we are unregistering unused MIME types and registering additional ones.
38
+ `More about MIME types. <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types >`_
39
+
40
+ .. literalinclude :: ../examples/httpserver_static_files_serving.py
41
+ :caption: examples/httpserver_static_files_serving.py
42
+ :linenos:
43
+
44
+ You can also serve a specific file from the handler.
45
+ By default ``Response.send_file() `` looks for the file in the server's ``root_path `` directory, but you can change it.
46
+
47
+ .. literalinclude :: ../examples/httpserver_handler_serves_file.py
48
+ :caption: examples/httpserver_handler_serves_file.py
49
+ :linenos:
50
+
51
+ Tasks in the background
52
+ -----------------------
53
+
10
54
If you want your code to do more than just serve web pages,
11
55
use the ``.start() ``/``.poll() `` methods as shown in this example.
12
56
13
57
Between calling ``.poll() `` you can do something useful,
14
58
for example read a sensor and capture an average or
15
59
a running total of the last 10 samples.
16
60
17
- .. literalinclude :: ../examples/httpserver_simple_poll .py
18
- :caption: examples/httpserver_simple_poll .py
61
+ .. literalinclude :: ../examples/httpserver_start_and_poll .py
62
+ :caption: examples/httpserver_start_and_poll .py
19
63
:linenos:
20
64
21
65
Server with MDNS
@@ -30,6 +74,22 @@ In this example, the server is accessible via ``http://custom-mdns-hostname/`` a
30
74
:caption: examples/httpserver_mdns.py
31
75
:linenos:
32
76
77
+ Handling different methods
78
+ ---------------------------------------
79
+
80
+ On every ``server.route() `` call you can specify which HTTP methods are allowed.
81
+ By default, only ``GET `` method is allowed.
82
+
83
+ You can pass a list of methods or a single method as a string.
84
+
85
+ It is recommended to use the the values in ``adafruit_httpserver.methods `` module to avoid typos and for future proofness.
86
+
87
+ In example below, handler for ``/api `` route will be called when any of ``GET ``, ``POST ``, ``PUT ``, ``DELETE `` methods is used.
88
+
89
+ .. literalinclude :: ../examples/httpserver_methods.py
90
+ :caption: examples/httpserver_methods.py
91
+ :linenos:
92
+
33
93
Change NeoPixel color
34
94
---------------------
35
95
@@ -45,7 +105,7 @@ Tested on ESP32-S2 Feather.
45
105
:linenos:
46
106
47
107
Get CPU information
48
- ---------------------
108
+ -------------------
49
109
50
110
You can return data from sensors or any computed value as JSON.
51
111
That makes it easy to use the data in other applications.
@@ -55,23 +115,23 @@ That makes it easy to use the data in other applications.
55
115
:linenos:
56
116
57
117
Chunked response
58
- ---------------------
118
+ ----------------
59
119
60
120
Library supports chunked responses. This is useful for streaming data.
61
- To use it, you need to set the ``chunked=True `` when creating a ``HTTPResponse `` object.
121
+ To use it, you need to set the ``chunked=True `` when creating a ``Response `` object.
62
122
63
123
.. literalinclude :: ../examples/httpserver_chunked.py
64
124
:caption: examples/httpserver_chunked.py
65
125
:linenos:
66
126
67
127
URL parameters
68
- ---------------------
128
+ --------------
69
129
70
130
Alternatively to using query parameters, you can use URL parameters.
71
131
72
- In order to use URL parameters, you need to wrap them inside ``<> `` in ``HTTPServer .route ``, e.g. ``<my_parameter> ``.
132
+ In order to use URL parameters, you need to wrap them inside ``<> `` in ``Server .route ``, e.g. ``<my_parameter> ``.
73
133
74
- All URL parameters are **passed as positional (not keyword) arguments ** to the handler function, in order they are specified in `` HTTPServer.route `` .
134
+ All URL parameters values are **passed as keyword arguments ** to the handler function.
75
135
76
136
Notice how the handler function in example below accepts two additional arguments : ``device_id `` and ``action ``.
77
137
@@ -80,11 +140,26 @@ make sure to add default values for all the ones that might not be passed.
80
140
In the example below the second route has only one URL parameter, so the ``action `` parameter has a default value.
81
141
82
142
Keep in mind that URL parameters are always passed as strings, so you need to convert them to the desired type.
83
- Also note that the names of the function parameters **do not have to match ** with the ones used in route, but they **must ** be in the same order.
84
- Look at the example below to see how the ``route_param_1 `` and ``route_param_1 `` are named differently in the handler function.
85
-
86
- Although it is possible, it makes more sense be consistent with the names of the parameters in the route and in the handler function.
143
+ Also note that the names of the function parameters **have to match ** with the ones used in route, but they **do not have to ** be in the same order.
87
144
88
145
.. literalinclude :: ../examples/httpserver_url_parameters.py
89
146
:caption: examples/httpserver_url_parameters.py
90
147
:linenos:
148
+
149
+ Authentication
150
+ --------------
151
+
152
+ In order to increase security of your server, you can use ``Basic `` and ``Bearer `` authentication.
153
+
154
+ If you want to apply authentication to the whole server, you need to call ``.require_authentication `` on ``Server `` instance.
155
+
156
+ .. literalinclude :: ../examples/httpserver_authentication_server.py
157
+ :caption: examples/httpserver_authentication_server.py
158
+ :linenos:
159
+
160
+ On the other hand, if you want to apply authentication to a set of routes, you need to call ``require_authentication `` function.
161
+ In both cases you can check if ``request `` is authenticated by calling ``check_authentication `` on it.
162
+
163
+ .. literalinclude :: ../examples/httpserver_authentication_handlers.py
164
+ :caption: examples/httpserver_authentication_handlers.py
165
+ :linenos:
0 commit comments