Skip to content

Commit e5bf831

Browse files
committed
Added Multiple servers example
1 parent eba7a91 commit e5bf831

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

docs/examples.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,19 @@ In both cases you can check if ``request`` is authenticated by calling ``check_a
183183
:caption: examples/httpserver_authentication_handlers.py
184184
:emphasize-lines: 9-15,21-25,33,44,57
185185
:linenos:
186+
187+
Multiple servers
188+
----------------
189+
190+
Although it is not the primary use case, it is possible to run multiple servers at the same time.
191+
In order to do that, you need to create multiple ``Server`` instances and call ``.start()`` and ``.poll()`` on each of them.
192+
Using ``.serve_forever()`` for this is not possible because of it's blocking behaviour.
193+
194+
Each server **must have a different port number**.
195+
196+
In combination with separate authentication and diffrent ``root_path`` this allows creating moderately complex setups.
197+
198+
.. literalinclude:: ../examples/httpserver_multiple_servers.py
199+
:caption: examples/httpserver_multiple_servers.py
200+
:emphasize-lines: 13-14,17,26,35-36,51-52,57-58
201+
:linenos:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SPDX-FileCopyrightText: 2022 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import socketpool
6+
import wifi
7+
8+
from adafruit_httpserver import Server, Request, Response
9+
10+
11+
pool = socketpool.SocketPool(wifi.radio)
12+
13+
bedroom_server = Server(pool, "/bedroom")
14+
office_server = Server(pool, "/office")
15+
16+
17+
@bedroom_server.route("/bedroom")
18+
def bedroom(request: Request):
19+
"""
20+
This route is registered only with ``bedroom_server``.
21+
"""
22+
with Response(request) as response:
23+
response.send("Hello from the bedroom!")
24+
25+
26+
@office_server.route("/office")
27+
def office(request: Request):
28+
"""
29+
This route is registered only with ``office_server``.
30+
"""
31+
with Response(request) as response:
32+
response.send("Hello from the office!")
33+
34+
35+
@bedroom_server.route("/home")
36+
@office_server.route("/home")
37+
def home(request: Request):
38+
"""
39+
This route is registered with both servers.
40+
"""
41+
with Response(request) as response:
42+
response.send("Hello from home!")
43+
44+
45+
id_address = str(wifi.radio.ipv4_address)
46+
47+
# Start the servers.
48+
49+
print(f"[bedroom_server] Listening on http://{id_address}:5000")
50+
print(f"[office_server] Listening on http://{id_address}:8000")
51+
bedroom_server.start(id_address, 5000)
52+
office_server.start(id_address, 8000)
53+
54+
while True:
55+
try:
56+
# Process any waiting requests for both servers.
57+
bedroom_server.poll()
58+
office_server.poll()
59+
except OSError as error:
60+
print(error)
61+
continue

0 commit comments

Comments
 (0)