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:
parent
dc9f83cd1a
commit
2f1c484c7b
1 changed files with 34 additions and 0 deletions
34
examples/httpserver_simpletest_connectionmanager.py
Normal file
34
examples/httpserver_simpletest_connectionmanager.py
Normal 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))
|
||||
Loading…
Reference in a new issue