adafruit-circuitpython-httpserver 4.5.6__tar.gz → 4.6.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.readthedocs.yaml +3 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/PKG-INFO +3 -2
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/README.rst +1 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_circuitpython_httpserver.egg-info/PKG-INFO +3 -2
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_circuitpython_httpserver.egg-info/SOURCES.txt +9 -3
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/__init__.py +1 -1
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/server.py +91 -10
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/conf.py +0 -1
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/examples.rst +70 -50
- adafruit_circuitpython_httpserver-4.6.1/docs/starting_methods.rst +82 -0
- adafruit_circuitpython_httpserver-4.6.1/docs/starting_methods.rst.license +3 -0
- adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_https.py +30 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_multiple_servers.py +3 -3
- adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_simpletest_auto_connection_manager.py +23 -0
- adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_simpletest_manual_ap.py +30 -0
- adafruit-circuitpython-httpserver-4.5.6/examples/httpserver_ethernet_simpletest.py → adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_simpletest_manual_ethernet.py +4 -3
- adafruit-circuitpython-httpserver-4.5.6/examples/httpserver_simpletest_manual.py → adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_simpletest_manual_wifi.py +6 -7
- adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_video_stream.py +128 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/pyproject.toml +1 -1
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.github/workflows/build.yml +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.github/workflows/failure-help-text.yml +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.github/workflows/release_gh.yml +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.github/workflows/release_pypi.yml +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.gitignore +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.pre-commit-config.yaml +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.pylintrc +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/CODE_OF_CONDUCT.md +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/LICENSE +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/LICENSES/CC-BY-4.0.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/LICENSES/MIT.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/LICENSES/Unlicense.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/README.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_circuitpython_httpserver.egg-info/dependency_links.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_circuitpython_httpserver.egg-info/requires.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_circuitpython_httpserver.egg-info/top_level.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/authentication.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/exceptions.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/headers.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/interfaces.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/methods.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/mime_types.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/request.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/response.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/route.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/adafruit_httpserver/status.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/_static/favicon.ico +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/_static/favicon.ico.license +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/api.rst +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/api.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/examples.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/index.rst +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/index.rst.license +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/directory_listing.tpl.html +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/home.html +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_authentication_handlers.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_authentication_server.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_chunked.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_cookies.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_cpu_information.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_cpython.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_form_data.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_handler_serves_file.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_mdns.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_methods.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_neopixel.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_redirects.py +0 -0
- /adafruit-circuitpython-httpserver-4.5.6/examples/httpserver_simpletest_auto.py → /adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_simpletest_auto_settings_toml.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_sse.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_start_and_poll.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_start_and_poll_asyncio.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_static_files_serving.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_templates.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_url_parameters.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/httpserver_websocket.py +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/examples/settings.toml +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/optional_requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/requirements.txt +0 -0
- {adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/setup.cfg +0 -0
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: adafruit-circuitpython-httpserver
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.6.1
|
|
4
4
|
Summary: Simple HTTP Server for CircuitPython
|
|
5
5
|
Author-email: Adafruit Industries <circuitpython@adafruit.com>
|
|
6
6
|
License: MIT
|
|
@@ -51,6 +51,7 @@ HTTP Server for CircuitPython.
|
|
|
51
51
|
- Supports URL parameters and wildcard URLs.
|
|
52
52
|
- Supports HTTP Basic and Bearer Authentication on both server and route per level.
|
|
53
53
|
- Supports Websockets and Server-Sent Events.
|
|
54
|
+
- Limited support for HTTPS (only on selected microcontrollers with enough memory e.g. ESP32-S3).
|
|
54
55
|
|
|
55
56
|
|
|
56
57
|
Dependencies
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/README.rst
RENAMED
|
@@ -32,6 +32,7 @@ HTTP Server for CircuitPython.
|
|
|
32
32
|
- Supports URL parameters and wildcard URLs.
|
|
33
33
|
- Supports HTTP Basic and Bearer Authentication on both server and route per level.
|
|
34
34
|
- Supports Websockets and Server-Sent Events.
|
|
35
|
+
- Limited support for HTTPS (only on selected microcontrollers with enough memory e.g. ESP32-S3).
|
|
35
36
|
|
|
36
37
|
|
|
37
38
|
Dependencies
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: adafruit-circuitpython-httpserver
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.6.1
|
|
4
4
|
Summary: Simple HTTP Server for CircuitPython
|
|
5
5
|
Author-email: Adafruit Industries <circuitpython@adafruit.com>
|
|
6
6
|
License: MIT
|
|
@@ -51,6 +51,7 @@ HTTP Server for CircuitPython.
|
|
|
51
51
|
- Supports URL parameters and wildcard URLs.
|
|
52
52
|
- Supports HTTP Basic and Bearer Authentication on both server and route per level.
|
|
53
53
|
- Supports Websockets and Server-Sent Events.
|
|
54
|
+
- Limited support for HTTPS (only on selected microcontrollers with enough memory e.g. ESP32-S3).
|
|
54
55
|
|
|
55
56
|
|
|
56
57
|
Dependencies
|
|
@@ -42,6 +42,8 @@ docs/examples.rst.license
|
|
|
42
42
|
docs/index.rst
|
|
43
43
|
docs/index.rst.license
|
|
44
44
|
docs/requirements.txt
|
|
45
|
+
docs/starting_methods.rst
|
|
46
|
+
docs/starting_methods.rst.license
|
|
45
47
|
docs/_static/favicon.ico
|
|
46
48
|
docs/_static/favicon.ico.license
|
|
47
49
|
examples/directory_listing.tpl.html
|
|
@@ -52,21 +54,25 @@ examples/httpserver_chunked.py
|
|
|
52
54
|
examples/httpserver_cookies.py
|
|
53
55
|
examples/httpserver_cpu_information.py
|
|
54
56
|
examples/httpserver_cpython.py
|
|
55
|
-
examples/httpserver_ethernet_simpletest.py
|
|
56
57
|
examples/httpserver_form_data.py
|
|
57
58
|
examples/httpserver_handler_serves_file.py
|
|
59
|
+
examples/httpserver_https.py
|
|
58
60
|
examples/httpserver_mdns.py
|
|
59
61
|
examples/httpserver_methods.py
|
|
60
62
|
examples/httpserver_multiple_servers.py
|
|
61
63
|
examples/httpserver_neopixel.py
|
|
62
64
|
examples/httpserver_redirects.py
|
|
63
|
-
examples/
|
|
64
|
-
examples/
|
|
65
|
+
examples/httpserver_simpletest_auto_connection_manager.py
|
|
66
|
+
examples/httpserver_simpletest_auto_settings_toml.py
|
|
67
|
+
examples/httpserver_simpletest_manual_ap.py
|
|
68
|
+
examples/httpserver_simpletest_manual_ethernet.py
|
|
69
|
+
examples/httpserver_simpletest_manual_wifi.py
|
|
65
70
|
examples/httpserver_sse.py
|
|
66
71
|
examples/httpserver_start_and_poll.py
|
|
67
72
|
examples/httpserver_start_and_poll_asyncio.py
|
|
68
73
|
examples/httpserver_static_files_serving.py
|
|
69
74
|
examples/httpserver_templates.py
|
|
70
75
|
examples/httpserver_url_parameters.py
|
|
76
|
+
examples/httpserver_video_stream.py
|
|
71
77
|
examples/httpserver_websocket.py
|
|
72
78
|
examples/settings.toml
|
|
@@ -12,6 +12,7 @@ try:
|
|
|
12
12
|
except ImportError:
|
|
13
13
|
pass
|
|
14
14
|
|
|
15
|
+
from ssl import SSLContext, create_default_context
|
|
15
16
|
from errno import EAGAIN, ECONNRESET, ETIMEDOUT
|
|
16
17
|
from sys import implementation
|
|
17
18
|
from time import monotonic, sleep
|
|
@@ -33,12 +34,18 @@ from .response import Response, FileResponse
|
|
|
33
34
|
from .route import Route
|
|
34
35
|
from .status import BAD_REQUEST_400, UNAUTHORIZED_401, FORBIDDEN_403, NOT_FOUND_404
|
|
35
36
|
|
|
37
|
+
if implementation.name != "circuitpython":
|
|
38
|
+
from ssl import Purpose, CERT_NONE, SSLError # pylint: disable=ungrouped-imports
|
|
39
|
+
|
|
36
40
|
|
|
37
41
|
NO_REQUEST = "no_request"
|
|
38
42
|
CONNECTION_TIMED_OUT = "connection_timed_out"
|
|
39
43
|
REQUEST_HANDLED_NO_RESPONSE = "request_handled_no_response"
|
|
40
44
|
REQUEST_HANDLED_RESPONSE_SENT = "request_handled_response_sent"
|
|
41
45
|
|
|
46
|
+
# CircuitPython does not have these error codes
|
|
47
|
+
MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE = -30592
|
|
48
|
+
|
|
42
49
|
|
|
43
50
|
class Server: # pylint: disable=too-many-instance-attributes
|
|
44
51
|
"""A basic socket-based HTTP server."""
|
|
@@ -52,8 +59,50 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
52
59
|
root_path: str
|
|
53
60
|
"""Root directory to serve files from. ``None`` if serving files is disabled."""
|
|
54
61
|
|
|
62
|
+
@staticmethod
|
|
63
|
+
def _validate_https_cert_provided(
|
|
64
|
+
certfile: Union[str, None], keyfile: Union[str, None]
|
|
65
|
+
) -> None:
|
|
66
|
+
if certfile is None or keyfile is None:
|
|
67
|
+
raise ValueError("Both certfile and keyfile must be specified for HTTPS")
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def _create_circuitpython_ssl_context(certfile: str, keyfile: str) -> SSLContext:
|
|
71
|
+
ssl_context = create_default_context()
|
|
72
|
+
|
|
73
|
+
ssl_context.load_verify_locations(cadata="")
|
|
74
|
+
ssl_context.load_cert_chain(certfile, keyfile)
|
|
75
|
+
|
|
76
|
+
return ssl_context
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def _create_cpython_ssl_context(certfile: str, keyfile: str) -> SSLContext:
|
|
80
|
+
ssl_context = create_default_context(purpose=Purpose.CLIENT_AUTH)
|
|
81
|
+
|
|
82
|
+
ssl_context.load_cert_chain(certfile, keyfile)
|
|
83
|
+
|
|
84
|
+
ssl_context.verify_mode = CERT_NONE
|
|
85
|
+
ssl_context.check_hostname = False
|
|
86
|
+
|
|
87
|
+
return ssl_context
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def _create_ssl_context(cls, certfile: str, keyfile: str) -> SSLContext:
|
|
91
|
+
return (
|
|
92
|
+
cls._create_circuitpython_ssl_context(certfile, keyfile)
|
|
93
|
+
if implementation.name == "circuitpython"
|
|
94
|
+
else cls._create_cpython_ssl_context(certfile, keyfile)
|
|
95
|
+
)
|
|
96
|
+
|
|
55
97
|
def __init__(
|
|
56
|
-
self,
|
|
98
|
+
self,
|
|
99
|
+
socket_source: _ISocketPool,
|
|
100
|
+
root_path: str = None,
|
|
101
|
+
*,
|
|
102
|
+
https: bool = False,
|
|
103
|
+
certfile: str = None,
|
|
104
|
+
keyfile: str = None,
|
|
105
|
+
debug: bool = False,
|
|
57
106
|
) -> None:
|
|
58
107
|
"""Create a server, and get it ready to run.
|
|
59
108
|
|
|
@@ -61,16 +110,30 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
61
110
|
in CircuitPython or the `socket` module in CPython.
|
|
62
111
|
:param str root_path: Root directory to serve files from
|
|
63
112
|
:param bool debug: Enables debug messages useful during development
|
|
113
|
+
:param bool https: If True, the server will use HTTPS
|
|
114
|
+
:param str certfile: Path to the certificate file, required if ``https`` is True
|
|
115
|
+
:param str keyfile: Path to the private key file, required if ``https`` is True
|
|
64
116
|
"""
|
|
65
|
-
self._auths = []
|
|
66
117
|
self._buffer = bytearray(1024)
|
|
67
118
|
self._timeout = 1
|
|
119
|
+
|
|
120
|
+
self._auths = []
|
|
68
121
|
self._routes: "List[Route]" = []
|
|
122
|
+
self.headers = Headers()
|
|
123
|
+
|
|
69
124
|
self._socket_source = socket_source
|
|
70
125
|
self._sock = None
|
|
71
|
-
|
|
126
|
+
|
|
72
127
|
self.host, self.port = None, None
|
|
73
128
|
self.root_path = root_path
|
|
129
|
+
self.https = https
|
|
130
|
+
|
|
131
|
+
if https:
|
|
132
|
+
self._validate_https_cert_provided(certfile, keyfile)
|
|
133
|
+
self._ssl_context = self._create_ssl_context(certfile, keyfile)
|
|
134
|
+
else:
|
|
135
|
+
self._ssl_context = None
|
|
136
|
+
|
|
74
137
|
if root_path in ["", "/"] and debug:
|
|
75
138
|
_debug_warning_exposed_files(root_path)
|
|
76
139
|
self.stopped = True
|
|
@@ -197,6 +260,7 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
197
260
|
@staticmethod
|
|
198
261
|
def _create_server_socket(
|
|
199
262
|
socket_source: _ISocketPool,
|
|
263
|
+
ssl_context: "SSLContext | None",
|
|
200
264
|
host: str,
|
|
201
265
|
port: int,
|
|
202
266
|
) -> _ISocket:
|
|
@@ -206,6 +270,9 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
206
270
|
if implementation.version >= (9,) or implementation.name != "circuitpython":
|
|
207
271
|
sock.setsockopt(socket_source.SOL_SOCKET, socket_source.SO_REUSEADDR, 1)
|
|
208
272
|
|
|
273
|
+
if ssl_context is not None:
|
|
274
|
+
sock = ssl_context.wrap_socket(sock, server_side=True)
|
|
275
|
+
|
|
209
276
|
sock.bind((host, port))
|
|
210
277
|
sock.listen(10)
|
|
211
278
|
sock.setblocking(False) # Non-blocking socket
|
|
@@ -225,7 +292,9 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
225
292
|
self.host, self.port = host, port
|
|
226
293
|
|
|
227
294
|
self.stopped = False
|
|
228
|
-
self._sock = self._create_server_socket(
|
|
295
|
+
self._sock = self._create_server_socket(
|
|
296
|
+
self._socket_source, self._ssl_context, host, port
|
|
297
|
+
)
|
|
229
298
|
|
|
230
299
|
if self.debug:
|
|
231
300
|
_debug_started_server(self)
|
|
@@ -386,7 +455,9 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
386
455
|
name, value
|
|
387
456
|
)
|
|
388
457
|
|
|
389
|
-
def poll(
|
|
458
|
+
def poll( # pylint: disable=too-many-branches,too-many-return-statements
|
|
459
|
+
self,
|
|
460
|
+
) -> str:
|
|
390
461
|
"""
|
|
391
462
|
Call this method inside your main loop to get the server to check for new incoming client
|
|
392
463
|
requests. When a request comes in, it will be handled by the handler function.
|
|
@@ -399,11 +470,12 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
399
470
|
|
|
400
471
|
conn = None
|
|
401
472
|
try:
|
|
473
|
+
if self.debug:
|
|
474
|
+
_debug_start_time = monotonic()
|
|
475
|
+
|
|
402
476
|
conn, client_address = self._sock.accept()
|
|
403
477
|
conn.settimeout(self._timeout)
|
|
404
478
|
|
|
405
|
-
_debug_start_time = monotonic()
|
|
406
|
-
|
|
407
479
|
# Receive the whole request
|
|
408
480
|
if (request := self._receive_request(conn, client_address)) is None:
|
|
409
481
|
conn.close()
|
|
@@ -424,9 +496,8 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
424
496
|
# Send the response
|
|
425
497
|
response._send() # pylint: disable=protected-access
|
|
426
498
|
|
|
427
|
-
_debug_end_time = monotonic()
|
|
428
|
-
|
|
429
499
|
if self.debug:
|
|
500
|
+
_debug_end_time = monotonic()
|
|
430
501
|
_debug_response_sent(response, _debug_end_time - _debug_start_time)
|
|
431
502
|
|
|
432
503
|
return REQUEST_HANDLED_RESPONSE_SENT
|
|
@@ -439,6 +510,15 @@ class Server: # pylint: disable=too-many-instance-attributes
|
|
|
439
510
|
# Connection reset by peer, try again later.
|
|
440
511
|
if error.errno == ECONNRESET:
|
|
441
512
|
return NO_REQUEST
|
|
513
|
+
# Handshake failed, try again later.
|
|
514
|
+
if error.errno == MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE:
|
|
515
|
+
return NO_REQUEST
|
|
516
|
+
|
|
517
|
+
# CPython specific SSL related errors
|
|
518
|
+
if implementation.name != "circuitpython" and isinstance(error, SSLError):
|
|
519
|
+
# Ignore unknown SSL certificate errors
|
|
520
|
+
if getattr(error, "reason", None) == "SSLV3_ALERT_CERTIFICATE_UNKNOWN":
|
|
521
|
+
return NO_REQUEST
|
|
442
522
|
|
|
443
523
|
if self.debug:
|
|
444
524
|
_debug_exception_in_handler(error)
|
|
@@ -547,9 +627,10 @@ def _debug_warning_exposed_files(root_path: str):
|
|
|
547
627
|
|
|
548
628
|
def _debug_started_server(server: "Server"):
|
|
549
629
|
"""Prints a message when the server starts."""
|
|
630
|
+
scheme = "https" if server.https else "http"
|
|
550
631
|
host, port = server.host, server.port
|
|
551
632
|
|
|
552
|
-
print(f"Started development server on
|
|
633
|
+
print(f"Started development server on {scheme}://{host}:{port}")
|
|
553
634
|
|
|
554
635
|
|
|
555
636
|
def _debug_response_sent(response: "Response", time_elapsed: float):
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/conf.py
RENAMED
|
@@ -116,7 +116,6 @@ napoleon_numpy_docstring = False
|
|
|
116
116
|
import sphinx_rtd_theme
|
|
117
117
|
|
|
118
118
|
html_theme = "sphinx_rtd_theme"
|
|
119
|
-
html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."]
|
|
120
119
|
|
|
121
120
|
# Add any paths that contain custom static files (such as style sheets) here,
|
|
122
121
|
# relative to this directory. They are copied after the builtin static files,
|
|
@@ -1,50 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
.. note::
|
|
2
|
+
All examples in this document are using ``Server`` in ``debug`` mode.
|
|
3
|
+
This mode is useful for development, but it is not recommended to use it in production.
|
|
4
|
+
More about Debug mode at the end of Examples section.
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
**More about Debug mode at the end of Examples section.**
|
|
6
|
+
Different ways of starting the server
|
|
7
|
+
-------------------------------------
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
There are several ways to start the server on CircuitPython, mostly depending on the device you are using and
|
|
10
|
+
whether you have access to external network.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
Functionally, all of them are the same, not features of the server are limited or disabled in any way.
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
:caption: examples/httpserver_simpletest_manual.py
|
|
15
|
-
:emphasize-lines: 12-17
|
|
16
|
-
:linenos:
|
|
17
|
-
|
|
18
|
-
It is also possible to use Ethernet instead of WiFi.
|
|
19
|
-
The only difference in usage is related to configuring the ``socket_source`` differently.
|
|
20
|
-
|
|
21
|
-
.. literalinclude:: ../examples/httpserver_ethernet_simpletest.py
|
|
22
|
-
:caption: examples/httpserver_ethernet_simpletest.py
|
|
23
|
-
:emphasize-lines: 13-23
|
|
24
|
-
:linenos:
|
|
14
|
+
Below you can find examples of different ways to start the server:
|
|
25
15
|
|
|
26
|
-
|
|
27
|
-
`it is possible to use the environment variables <https://docs.circuitpython.org/en/latest/docs/environment.html#circuitpython-behavior>`_
|
|
28
|
-
defined in ``settings.toml`` file to store secrets and configure the WiFi network.
|
|
16
|
+
.. toctree::
|
|
29
17
|
|
|
30
|
-
|
|
31
|
-
If you want to use port ``80`` , you need to set ``CIRCUITPY_WEB_API_PORT`` to any other port, and then set ``port`` parameter in ``Server`` constructor to ``80`` .
|
|
32
|
-
|
|
33
|
-
This is the same example as above, but it uses the ``settings.toml`` file to configure the WiFi network.
|
|
34
|
-
|
|
35
|
-
**From now on, all the examples will use the** ``settings.toml`` **file to configure the WiFi network.**
|
|
36
|
-
|
|
37
|
-
.. literalinclude:: ../examples/settings.toml
|
|
38
|
-
:caption: settings.toml
|
|
39
|
-
:lines: 5-
|
|
40
|
-
:linenos:
|
|
41
|
-
|
|
42
|
-
Note that we still need to import ``socketpool`` and ``wifi`` modules.
|
|
43
|
-
|
|
44
|
-
.. literalinclude:: ../examples/httpserver_simpletest_auto.py
|
|
45
|
-
:caption: examples/httpserver_simpletest_auto.py
|
|
46
|
-
:emphasize-lines: 11
|
|
47
|
-
:linenos:
|
|
18
|
+
starting_methods
|
|
48
19
|
|
|
49
20
|
CPython usage
|
|
50
21
|
--------------------
|
|
@@ -210,7 +181,7 @@ You can find more information about the template syntax in the
|
|
|
210
181
|
|
|
211
182
|
.. literalinclude:: ../examples/httpserver_templates.py
|
|
212
183
|
:caption: examples/httpserver_templates.py
|
|
213
|
-
:emphasize-lines: 12-15,
|
|
184
|
+
:emphasize-lines: 12-15,51-59
|
|
214
185
|
:linenos:
|
|
215
186
|
|
|
216
187
|
Form data parsing
|
|
@@ -225,7 +196,7 @@ It is important to use correct ``enctype``, depending on the type of data you wa
|
|
|
225
196
|
- ``application/x-www-form-urlencoded`` - For sending simple text data without any special characters including spaces.
|
|
226
197
|
If you use it, values will be automatically parsed as strings, but special characters will be URL encoded
|
|
227
198
|
e.g. ``"Hello World! ^-$%"`` will be saved as ``"Hello+World%21+%5E-%24%25"``
|
|
228
|
-
- ``multipart/form-data`` - For sending
|
|
199
|
+
- ``multipart/form-data`` - For sending text with special characters and files
|
|
229
200
|
When used, non-file values will be automatically parsed as strings and non plain text files will be saved as ``bytes``.
|
|
230
201
|
e.g. ``"Hello World! ^-$%"`` will be saved as ``'Hello World! ^-$%'``, and e.g. a PNG file will be saved as ``b'\x89PNG\r\n\x1a\n\x00\...``.
|
|
231
202
|
- ``text/plain`` - For sending text data with special characters.
|
|
@@ -351,8 +322,10 @@ This can be overcomed by periodically polling the server, but it is not an elega
|
|
|
351
322
|
Response is initialized on ``return``, events can be sent using ``.send_event()`` method. Due to the nature of SSE, it is necessary to store the
|
|
352
323
|
response object somewhere, so that it can be accessed later.
|
|
353
324
|
|
|
354
|
-
|
|
355
|
-
|
|
325
|
+
|
|
326
|
+
.. warning::
|
|
327
|
+
Because of the limited number of concurrently open sockets, it is **not possible to process more than one SSE response at the same time**.
|
|
328
|
+
This might change in the future, but for now, it is recommended to use SSE **only with one client at a time**.
|
|
356
329
|
|
|
357
330
|
.. literalinclude:: ../examples/httpserver_sse.py
|
|
358
331
|
:caption: examples/httpserver_sse.py
|
|
@@ -373,14 +346,61 @@ This is anologous to calling ``.poll()`` on the ``Server`` object.
|
|
|
373
346
|
The following example uses ``asyncio``, which has to be installed separately. It is not necessary to use ``asyncio`` to use Websockets,
|
|
374
347
|
but it is recommended as it makes it easier to handle multiple tasks. It can be used in any of the examples, but here it is particularly useful.
|
|
375
348
|
|
|
376
|
-
|
|
377
|
-
|
|
349
|
+
.. warning::
|
|
350
|
+
Because of the limited number of concurrently open sockets, it is **not possible to process more than one Websocket response at the same time**.
|
|
351
|
+
This might change in the future, but for now, it is recommended to use Websocket **only with one client at a time**.
|
|
378
352
|
|
|
379
353
|
.. literalinclude:: ../examples/httpserver_websocket.py
|
|
380
354
|
:caption: examples/httpserver_websocket.py
|
|
381
355
|
:emphasize-lines: 12,20,65-72,88,99
|
|
382
356
|
:linenos:
|
|
383
357
|
|
|
358
|
+
Custom response types e.g. video streaming
|
|
359
|
+
------------------------------------------
|
|
360
|
+
|
|
361
|
+
The built-in response types may not always meet your specific requirements. In such cases, you can define custom response types and implement
|
|
362
|
+
the necessary logic.
|
|
363
|
+
|
|
364
|
+
The example below demonstrates a ``XMixedReplaceResponse`` class, which uses the ``multipart/x-mixed-replace`` content type to stream video frames
|
|
365
|
+
from a camera, similar to a CCTV system.
|
|
366
|
+
|
|
367
|
+
To ensure the server remains responsive, a global list of open connections is maintained. By running tasks asynchronously, the server can stream
|
|
368
|
+
video to multiple clients while simultaneously handling other requests.
|
|
369
|
+
|
|
370
|
+
.. literalinclude:: ../examples/httpserver_video_stream.py
|
|
371
|
+
:caption: examples/httpserver_video_stream.py
|
|
372
|
+
:emphasize-lines: 31-77,92
|
|
373
|
+
:linenos:
|
|
374
|
+
|
|
375
|
+
HTTPS
|
|
376
|
+
-----
|
|
377
|
+
|
|
378
|
+
.. warning::
|
|
379
|
+
HTTPS on CircuitPython **works only on boards with enough memory e.g. ESP32-S3**.
|
|
380
|
+
|
|
381
|
+
When you want to expose your server to the internet or an untrusted network, it is recommended to use HTTPS.
|
|
382
|
+
Together with authentication, it provides a relatively secure way to communicate with the server.
|
|
383
|
+
|
|
384
|
+
.. note::
|
|
385
|
+
Using HTTPS slows down the server, because of additional work with encryption and decryption.
|
|
386
|
+
|
|
387
|
+
Enabling HTTPS is straightforward and comes down to passing the path to the certificate and key files to the ``Server`` constructor
|
|
388
|
+
and setting ``https=True``.
|
|
389
|
+
|
|
390
|
+
.. literalinclude:: ../examples/httpserver_https.py
|
|
391
|
+
:caption: examples/httpserver_https.py
|
|
392
|
+
:emphasize-lines: 15-17
|
|
393
|
+
:linenos:
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
To create your own certificate, you can use the following command:
|
|
397
|
+
|
|
398
|
+
.. code-block:: bash
|
|
399
|
+
|
|
400
|
+
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout key.pem -out cert.pem
|
|
401
|
+
|
|
402
|
+
You might have to change permissions of the files, so that the server can read them.
|
|
403
|
+
|
|
384
404
|
Multiple servers
|
|
385
405
|
----------------
|
|
386
406
|
|
|
@@ -390,7 +410,7 @@ Using ``.serve_forever()`` for this is not possible because of it's blocking beh
|
|
|
390
410
|
|
|
391
411
|
Each server **must have a different port number**.
|
|
392
412
|
|
|
393
|
-
|
|
413
|
+
To distinguish between responses from different servers a 'X-Server' header is added to each response.
|
|
394
414
|
**This is an optional step**, both servers will work without it.
|
|
395
415
|
|
|
396
416
|
In combination with separate authentication and diffrent ``root_path`` this allows creating moderately complex setups.
|
|
@@ -433,5 +453,5 @@ This is the default format of the logs::
|
|
|
433
453
|
If you need more information about the server or request, or you want it in a different format you can modify
|
|
434
454
|
functions at the bottom of ``adafruit_httpserver/server.py`` that start with ``_debug_...``.
|
|
435
455
|
|
|
436
|
-
|
|
437
|
-
|
|
456
|
+
.. note::
|
|
457
|
+
This is an advanced usage that might change in the future. It is not recommended to modify other parts of the code.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
|
|
2
|
+
Manual WiFi
|
|
3
|
+
-----------
|
|
4
|
+
|
|
5
|
+
This is the minimal example of using the library with CircuitPython.
|
|
6
|
+
This example is serving a simple static text message.
|
|
7
|
+
|
|
8
|
+
It also manually connects to the WiFi network. SSID and password are stored in the code, but they
|
|
9
|
+
can as well be stored in the ``settings.toml`` file, and then read from there using ``os.getenv()``.
|
|
10
|
+
|
|
11
|
+
.. literalinclude:: ../examples/httpserver_simpletest_manual_wifi.py
|
|
12
|
+
:caption: examples/httpserver_simpletest_manual_wifi.py
|
|
13
|
+
:emphasize-lines: 10-17
|
|
14
|
+
:linenos:
|
|
15
|
+
|
|
16
|
+
Manual AP (access point)
|
|
17
|
+
------------------------
|
|
18
|
+
|
|
19
|
+
If there is no external network available, it is possible to create an access point (AP) and run a server on it.
|
|
20
|
+
It is important to note that only devices connected to the AP will be able to access the server and depending on the device,
|
|
21
|
+
it may not be able to access the internet.
|
|
22
|
+
|
|
23
|
+
.. literalinclude:: ../examples/httpserver_simpletest_manual_ap.py
|
|
24
|
+
:caption: examples/httpserver_simpletest_manual_ap.py
|
|
25
|
+
:emphasize-lines: 11-16,30
|
|
26
|
+
:linenos:
|
|
27
|
+
|
|
28
|
+
Manual Ethernet
|
|
29
|
+
---------------
|
|
30
|
+
|
|
31
|
+
Most of the time, the WiFi will be a preferred way of connecting to the network.
|
|
32
|
+
Nevertheless it is also possible to use Ethernet instead of WiFi.
|
|
33
|
+
The only difference in usage is related to configuring the ``socket_source`` differently.
|
|
34
|
+
|
|
35
|
+
.. literalinclude:: ../examples/httpserver_simpletest_manual_ethernet.py
|
|
36
|
+
:caption: examples/httpserver_simpletest_manual_ethernet.py
|
|
37
|
+
:emphasize-lines: 9-10,13-25,38
|
|
38
|
+
:linenos:
|
|
39
|
+
|
|
40
|
+
Automatic WiFi using ``settings.toml``
|
|
41
|
+
--------------------------------------
|
|
42
|
+
|
|
43
|
+
From the version 8.0.0 of CircuitPython,
|
|
44
|
+
`it is possible to use the environment variables <https://docs.circuitpython.org/en/latest/docs/environment.html#circuitpython-behavior>`_
|
|
45
|
+
defined in ``settings.toml`` file to store secrets and configure the WiFi network
|
|
46
|
+
using the ``CIRCUITPY_WIFI_SSID`` and ``CIRCUITPY_WIFI_PASSWORD`` variables.
|
|
47
|
+
|
|
48
|
+
By default the library uses ``0.0.0.0`` and port ``5000`` for the server, as port ``80`` is reserved for the CircuitPython Web Workflow.
|
|
49
|
+
If you want to use port ``80`` , you need to set ``CIRCUITPY_WEB_API_PORT`` to any other port, and then set ``port`` parameter in ``Server`` constructor to ``80`` .
|
|
50
|
+
|
|
51
|
+
This is the same example as above, but it uses the ``settings.toml`` file to configure the WiFi network.
|
|
52
|
+
|
|
53
|
+
.. note::
|
|
54
|
+
From now on, all the examples will use the ``settings.toml`` file to configure the WiFi network.
|
|
55
|
+
|
|
56
|
+
.. literalinclude:: ../examples/settings.toml
|
|
57
|
+
:caption: settings.toml
|
|
58
|
+
:lines: 5-
|
|
59
|
+
:linenos:
|
|
60
|
+
|
|
61
|
+
Note that we still need to import ``socketpool`` and ``wifi`` modules.
|
|
62
|
+
|
|
63
|
+
.. literalinclude:: ../examples/httpserver_simpletest_auto_settings_toml.py
|
|
64
|
+
:caption: examples/httpserver_simpletest_auto_settings_toml.py
|
|
65
|
+
:emphasize-lines: 11
|
|
66
|
+
:linenos:
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
Helper for socket pool using ``adafruit_connection_manager``
|
|
70
|
+
------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
If you do not want to configure the socket pool manually, you can use the ``adafruit_connection_manager`` library,
|
|
73
|
+
which provides helpers for getting socket pool and SSL context for common boards.
|
|
74
|
+
|
|
75
|
+
Note that it is not installed by default.
|
|
76
|
+
You can read `more about it here <https://docs.circuitpython.org/projects/connectionmanager/en/latest/index.html>`_.
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
.. literalinclude:: ../examples/httpserver_simpletest_auto_connection_manager.py
|
|
80
|
+
:caption: examples/httpserver_simpletest_auto_connection_manager.py
|
|
81
|
+
:emphasize-lines: 7,11
|
|
82
|
+
:linenos:
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 Michał Pokusa
|
|
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
|
+
server = Server(
|
|
13
|
+
pool,
|
|
14
|
+
root_path="/static",
|
|
15
|
+
https=True,
|
|
16
|
+
certfile="cert.pem",
|
|
17
|
+
keyfile="key.pem",
|
|
18
|
+
debug=True,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@server.route("/")
|
|
23
|
+
def base(request: Request):
|
|
24
|
+
"""
|
|
25
|
+
Serve a default static plain text message.
|
|
26
|
+
"""
|
|
27
|
+
return Response(request, "Hello from the CircuitPython HTTPS Server!")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
server.serve_forever(str(wifi.radio.ipv4_address), 443)
|
|
@@ -42,11 +42,11 @@ def home(request: Request):
|
|
|
42
42
|
return Response(request, "Hello from home!")
|
|
43
43
|
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
ip_address = str(wifi.radio.ipv4_address)
|
|
46
46
|
|
|
47
47
|
# Start the servers.
|
|
48
|
-
bedroom_server.start(
|
|
49
|
-
office_server.start(
|
|
48
|
+
bedroom_server.start(ip_address, 5000)
|
|
49
|
+
office_server.start(ip_address, 8000)
|
|
50
50
|
|
|
51
51
|
while True:
|
|
52
52
|
try:
|
adafruit_circuitpython_httpserver-4.6.1/examples/httpserver_simpletest_auto_connection_manager.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 DJDevon3
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
import wifi
|
|
6
|
+
|
|
7
|
+
from adafruit_connection_manager import get_radio_socketpool
|
|
8
|
+
from adafruit_httpserver import Server, Request, Response
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
pool = get_radio_socketpool(wifi.radio)
|
|
12
|
+
server = Server(pool, "/static", debug=True)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@server.route("/")
|
|
16
|
+
def base(request: Request):
|
|
17
|
+
"""
|
|
18
|
+
Serve a default static plain text message.
|
|
19
|
+
"""
|
|
20
|
+
return Response(request, "Hello from the CircuitPython HTTP Server!")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
server.serve_forever(str(wifi.radio.ipv4_address))
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 Michał Pokusa
|
|
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
|
+
AP_SSID = "..."
|
|
12
|
+
AP_PASSWORD = "..."
|
|
13
|
+
|
|
14
|
+
print("Creating access point...")
|
|
15
|
+
wifi.radio.start_ap(ssid=AP_SSID, password=AP_PASSWORD)
|
|
16
|
+
print(f"Created access point {AP_SSID}")
|
|
17
|
+
|
|
18
|
+
pool = socketpool.SocketPool(wifi.radio)
|
|
19
|
+
server = Server(pool, "/static", debug=True)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@server.route("/")
|
|
23
|
+
def base(request: Request):
|
|
24
|
+
"""
|
|
25
|
+
Serve a default static plain text message.
|
|
26
|
+
"""
|
|
27
|
+
return Response(request, "Hello from the CircuitPython HTTP Server!")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
server.serve_forever(str(wifi.radio.ipv4_address_ap))
|
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries
|
|
2
|
+
#
|
|
2
3
|
# SPDX-License-Identifier: MIT
|
|
3
4
|
|
|
4
5
|
import board
|
|
5
6
|
import digitalio
|
|
6
7
|
|
|
7
8
|
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
|
|
8
|
-
import
|
|
9
|
+
from adafruit_wiznet5k import adafruit_wiznet5k_socket as socket
|
|
9
10
|
from adafruit_httpserver import Server, Request, Response
|
|
10
11
|
|
|
11
|
-
print("Wiznet5k HTTPServer Test")
|
|
12
12
|
|
|
13
13
|
# For Adafruit Ethernet FeatherWing
|
|
14
14
|
cs = digitalio.DigitalInOut(board.D10)
|
|
15
|
+
|
|
15
16
|
# For Particle Ethernet FeatherWing
|
|
16
17
|
# cs = digitalio.DigitalInOut(board.D5)
|
|
18
|
+
|
|
17
19
|
spi_bus = board.SPI()
|
|
18
20
|
|
|
19
21
|
# Initialize ethernet interface with DHCP
|
|
@@ -22,7 +24,6 @@ eth = WIZNET5K(spi_bus, cs)
|
|
|
22
24
|
# Set the interface on the socket source
|
|
23
25
|
socket.set_interface(eth)
|
|
24
26
|
|
|
25
|
-
# Initialize the server
|
|
26
27
|
server = Server(socket, "/static", debug=True)
|
|
27
28
|
|
|
28
29
|
|
|
@@ -2,21 +2,20 @@
|
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Unlicense
|
|
4
4
|
|
|
5
|
-
import os
|
|
6
|
-
|
|
7
5
|
import socketpool
|
|
8
6
|
import wifi
|
|
9
7
|
|
|
10
8
|
from adafruit_httpserver import Server, Request, Response
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
WIFI_SSID = "..."
|
|
11
|
+
WIFI_PASSWORD = "..."
|
|
14
12
|
|
|
15
|
-
print("Connecting to"
|
|
16
|
-
wifi.radio.connect(
|
|
17
|
-
print("Connected to"
|
|
13
|
+
print(f"Connecting to {WIFI_SSID}...")
|
|
14
|
+
wifi.radio.connect(WIFI_SSID, WIFI_PASSWORD)
|
|
15
|
+
print(f"Connected to {WIFI_SSID}")
|
|
18
16
|
|
|
19
17
|
pool = socketpool.SocketPool(wifi.radio)
|
|
18
|
+
|
|
20
19
|
server = Server(pool, "/static", debug=True)
|
|
21
20
|
|
|
22
21
|
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2024 Michał Pokusa
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Unlicense
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
from typing import Dict, List, Tuple, Union
|
|
7
|
+
except ImportError:
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
from asyncio import create_task, gather, run, sleep
|
|
11
|
+
from random import choice
|
|
12
|
+
|
|
13
|
+
import socketpool
|
|
14
|
+
import wifi
|
|
15
|
+
|
|
16
|
+
from adafruit_pycamera import PyCamera
|
|
17
|
+
from adafruit_httpserver import Server, Request, Response, Headers, Status, OK_200
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
pool = socketpool.SocketPool(wifi.radio)
|
|
21
|
+
server = Server(pool, debug=True)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
camera = PyCamera()
|
|
25
|
+
camera.display.brightness = 0
|
|
26
|
+
camera.mode = 0 # JPEG, required for `capture_into_jpeg()`
|
|
27
|
+
camera.resolution = "1280x720"
|
|
28
|
+
camera.effect = 0 # No effect
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class XMixedReplaceResponse(Response):
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
request: Request,
|
|
35
|
+
frame_content_type: str,
|
|
36
|
+
*,
|
|
37
|
+
status: Union[Status, Tuple[int, str]] = OK_200,
|
|
38
|
+
headers: Union[Headers, Dict[str, str]] = None,
|
|
39
|
+
cookies: Dict[str, str] = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
super().__init__(
|
|
42
|
+
request=request,
|
|
43
|
+
headers=headers,
|
|
44
|
+
cookies=cookies,
|
|
45
|
+
status=status,
|
|
46
|
+
)
|
|
47
|
+
self._boundary = self._get_random_boundary()
|
|
48
|
+
self._headers.setdefault(
|
|
49
|
+
"Content-Type", f"multipart/x-mixed-replace; boundary={self._boundary}"
|
|
50
|
+
)
|
|
51
|
+
self._frame_content_type = frame_content_type
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def _get_random_boundary() -> str:
|
|
55
|
+
symbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
56
|
+
return "--" + "".join([choice(symbols) for _ in range(16)])
|
|
57
|
+
|
|
58
|
+
def send_frame(self, frame: Union[str, bytes] = "") -> None:
|
|
59
|
+
encoded_frame = bytes(
|
|
60
|
+
frame.encode("utf-8") if isinstance(frame, str) else frame
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
self._send_bytes(
|
|
64
|
+
self._request.connection, bytes(f"{self._boundary}\r\n", "utf-8")
|
|
65
|
+
)
|
|
66
|
+
self._send_bytes(
|
|
67
|
+
self._request.connection,
|
|
68
|
+
bytes(f"Content-Type: {self._frame_content_type}\r\n\r\n", "utf-8"),
|
|
69
|
+
)
|
|
70
|
+
self._send_bytes(self._request.connection, encoded_frame)
|
|
71
|
+
self._send_bytes(self._request.connection, bytes("\r\n", "utf-8"))
|
|
72
|
+
|
|
73
|
+
def _send(self) -> None:
|
|
74
|
+
self._send_headers()
|
|
75
|
+
|
|
76
|
+
def close(self) -> None:
|
|
77
|
+
self._close_connection()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
stream_connections: List[XMixedReplaceResponse] = []
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@server.route("/frame")
|
|
84
|
+
def frame_handler(request: Request):
|
|
85
|
+
frame = camera.capture_into_jpeg()
|
|
86
|
+
|
|
87
|
+
return Response(request, body=frame, content_type="image/jpeg")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@server.route("/stream")
|
|
91
|
+
def stream_handler(request: Request):
|
|
92
|
+
response = XMixedReplaceResponse(request, frame_content_type="image/jpeg")
|
|
93
|
+
stream_connections.append(response)
|
|
94
|
+
|
|
95
|
+
return response
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def send_stream_frames():
|
|
99
|
+
while True:
|
|
100
|
+
await sleep(0.1)
|
|
101
|
+
|
|
102
|
+
frame = camera.capture_into_jpeg()
|
|
103
|
+
|
|
104
|
+
for connection in iter(stream_connections):
|
|
105
|
+
try:
|
|
106
|
+
connection.send_frame(frame)
|
|
107
|
+
except BrokenPipeError:
|
|
108
|
+
connection.close()
|
|
109
|
+
stream_connections.remove(connection)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
async def handle_http_requests():
|
|
113
|
+
server.start(str(wifi.radio.ipv4_address))
|
|
114
|
+
|
|
115
|
+
while True:
|
|
116
|
+
await sleep(0)
|
|
117
|
+
|
|
118
|
+
server.poll()
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
async def main():
|
|
122
|
+
await gather(
|
|
123
|
+
create_task(send_stream_frames()),
|
|
124
|
+
create_task(handle_http_requests()),
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
run(main())
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/pyproject.toml
RENAMED
|
@@ -12,7 +12,7 @@ requires = [
|
|
|
12
12
|
[project]
|
|
13
13
|
name = "adafruit-circuitpython-httpserver"
|
|
14
14
|
description = "Simple HTTP Server for CircuitPython"
|
|
15
|
-
version = "4.
|
|
15
|
+
version = "4.6.1"
|
|
16
16
|
readme = "README.rst"
|
|
17
17
|
authors = [
|
|
18
18
|
{name = "Adafruit Industries", email = "circuitpython@adafruit.com"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.gitignore
RENAMED
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/.pylintrc
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/LICENSES/MIT.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/api.rst
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/docs/index.rst
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/requirements.txt
RENAMED
|
File without changes
|
{adafruit-circuitpython-httpserver-4.5.6 → adafruit_circuitpython_httpserver-4.6.1}/setup.cfg
RENAMED
|
File without changes
|