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.
This commit is contained in:
DJDevon3 2024-03-26 23:50:47 -04:00
parent dc9f83cd1a
commit 2f1c484c7b

View file

@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: 2024 DJDevon3
# SPDX-License-Identifier: MIT
# Coded for Circuit Python 9.
"""HTTP Server Simpletest with Connection Manager"""
# pylint: disable=import-error
import os
import adafruit_connection_manager
import wifi
from adafruit_httpserver import Server, Request, Response
# Get WiFi details, ensure these are setup in settings.toml
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
print("Connecting to WiFi...")
wifi.radio.connect(ssid, password)
print("✅ Wifi!")
# Initalize Wifi, Socket Pool, Request Session
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
server = Server(pool, "/static", debug=True)
@server.route("/")
def base(request: Request):
"""Serve a default static plain text message"""
return Response(request, "Hello from the CircuitPython HTTP Server!")
server.serve_forever(str(wifi.radio.ipv4_address))