@@ -8,6 +8,7 @@ It also manually connects to the WiFi network.
8
8
9
9
.. literalinclude :: ../examples/httpserver_simpletest_manual.py
10
10
:caption: examples/httpserver_simpletest_manual.py
11
+ :emphasize-lines: 12-17
11
12
:linenos:
12
13
13
14
Although there is nothing wrong with this approach, from the version 8.0.0 of CircuitPython,
@@ -39,13 +40,15 @@ In order to save memory, we are unregistering unused MIME types and registering
39
40
40
41
.. literalinclude :: ../examples/httpserver_static_files_serving.py
41
42
:caption: examples/httpserver_static_files_serving.py
43
+ :emphasize-lines: 12-18,23-26
42
44
:linenos:
43
45
44
46
You can also serve a specific file from the handler.
45
47
By default ``Response.send_file() `` looks for the file in the server's ``root_path `` directory, but you can change it.
46
48
47
49
.. literalinclude :: ../examples/httpserver_handler_serves_file.py
48
50
:caption: examples/httpserver_handler_serves_file.py
51
+ :emphasize-lines: 22-23
49
52
:linenos:
50
53
51
54
.. literalinclude :: ../examples/home.html
@@ -65,6 +68,7 @@ a running total of the last 10 samples.
65
68
66
69
.. literalinclude :: ../examples/httpserver_start_and_poll.py
67
70
:caption: examples/httpserver_start_and_poll.py
71
+ :emphasize-lines: 26-39
68
72
:linenos:
69
73
70
74
Server with MDNS
@@ -77,6 +81,7 @@ In this example, the server is accessible via ``http://custom-mdns-hostname/`` a
77
81
78
82
.. literalinclude :: ../examples/httpserver_mdns.py
79
83
:caption: examples/httpserver_mdns.py
84
+ :emphasize-lines: 12-14
80
85
:linenos:
81
86
82
87
Handling different methods
@@ -93,20 +98,23 @@ In example below, handler for ``/api`` route will be called when any of ``GET``,
93
98
94
99
.. literalinclude :: ../examples/httpserver_methods.py
95
100
:caption: examples/httpserver_methods.py
101
+ :emphasize-lines: 8,15
96
102
:linenos:
97
103
98
104
Change NeoPixel color
99
105
---------------------
100
106
101
- If you want your code to do more than just serve web pages,
102
- use the start/poll methods as shown in this example.
107
+ In your handler function you can access the query/GET parameters using ``request.query_params ``.
108
+ This allows you to pass data to the handler function and use it in your code.
109
+ Alternatively you can use URL parameters, which are described later in this document.
103
110
104
111
For example by going to ``/change-neopixel-color?r=255&g=0&b=0 `` or ``/change-neopixel-color/255/0/0 ``
105
112
you can change the color of the NeoPixel to red.
106
113
Tested on ESP32-S2 Feather.
107
114
108
115
.. literalinclude :: ../examples/httpserver_neopixel.py
109
116
:caption: examples/httpserver_neopixel.py
117
+ :emphasize-lines: 24-26,34-35
110
118
:linenos:
111
119
112
120
Get CPU information
@@ -117,6 +125,7 @@ That makes it easy to use the data in other applications.
117
125
118
126
.. literalinclude :: ../examples/httpserver_cpu_information.py
119
127
:caption: examples/httpserver_cpu_information.py
128
+ :emphasize-lines: 28-29
120
129
:linenos:
121
130
122
131
Chunked response
@@ -127,6 +136,7 @@ To use it, you need to set the ``chunked=True`` when creating a ``Response`` obj
127
136
128
137
.. literalinclude :: ../examples/httpserver_chunked.py
129
138
:caption: examples/httpserver_chunked.py
139
+ :emphasize-lines: 21-26
130
140
:linenos:
131
141
132
142
URL parameters
@@ -149,6 +159,7 @@ Also note that the names of the function parameters **have to match** with the o
149
159
150
160
.. literalinclude :: ../examples/httpserver_url_parameters.py
151
161
:caption: examples/httpserver_url_parameters.py
162
+ :emphasize-lines: 30-34
152
163
:linenos:
153
164
154
165
Authentication
@@ -160,11 +171,13 @@ If you want to apply authentication to the whole server, you need to call ``.req
160
171
161
172
.. literalinclude :: ../examples/httpserver_authentication_server.py
162
173
:caption: examples/httpserver_authentication_server.py
174
+ :emphasize-lines: 8,11-15,19
163
175
:linenos:
164
176
165
177
On the other hand, if you want to apply authentication to a set of routes, you need to call ``require_authentication `` function.
166
178
In both cases you can check if ``request `` is authenticated by calling ``check_authentication `` on it.
167
179
168
180
.. literalinclude :: ../examples/httpserver_authentication_handlers.py
169
181
:caption: examples/httpserver_authentication_handlers.py
182
+ :emphasize-lines: 9-15,21-25,33,44,57
170
183
:linenos:
0 commit comments