Skip to content

Commit 2f1c484

Browse files
committed
Add httpserver simpletest with connection manager
Only handles setup for socketpool. Coming from requests examples I'm more used to seeing connection manager handle the pool now. It might be good for consistency to role out connection manager for all examples but I'll leave that up to you.
1 parent dc9f83c commit 2f1c484

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: 2024 DJDevon3
2+
# SPDX-License-Identifier: MIT
3+
# Coded for Circuit Python 9.
4+
"""HTTP Server Simpletest with Connection Manager"""
5+
# pylint: disable=import-error
6+
7+
import os
8+
9+
import adafruit_connection_manager
10+
import wifi
11+
12+
from adafruit_httpserver import Server, Request, Response
13+
14+
# Get WiFi details, ensure these are setup in settings.toml
15+
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
16+
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
17+
18+
print("Connecting to WiFi...")
19+
wifi.radio.connect(ssid, password)
20+
print("✅ Wifi!")
21+
22+
# Initalize Wifi, Socket Pool, Request Session
23+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
24+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
25+
server = Server(pool, "/static", debug=True)
26+
27+
28+
@server.route("/")
29+
def base(request: Request):
30+
"""Serve a default static plain text message"""
31+
return Response(request, "Hello from the CircuitPython HTTP Server!")
32+
33+
34+
server.serve_forever(str(wifi.radio.ipv4_address))

0 commit comments

Comments
 (0)