Skip to content

Commit e4c05ad

Browse files
committed
Added example of using lib with CPython
1 parent f417d34 commit e4c05ad

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

docs/examples.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Simple Test
55
**This mode is useful for development, but it is not recommended to use it in production.**
66
**More about Debug mode at the end of Examples section.**
77

8-
This is the minimal example of using the library.
8+
This is the minimal example of using the library with CircuitPython.
99
This example is serving a simple static text message.
1010

1111
It also manually connects to the WiFi network.
@@ -43,6 +43,17 @@ Note that we still need to import ``socketpool`` and ``wifi`` modules.
4343
:emphasize-lines: 11
4444
:linenos:
4545

46+
CPython usage
47+
--------------------
48+
49+
Library can also be used in CPython, no changes other than changing the ``socket_source`` are necessary.
50+
51+
.. literalinclude:: ../examples/httpserver_cpython.py
52+
:caption: examples/httpserver_cpython.py
53+
:emphasize-lines: 5,10
54+
:linenos:
55+
56+
4657
Serving static files
4758
--------------------
4859

examples/httpserver_cpython.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2024 Michał Pokusa
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import socket
6+
7+
from adafruit_httpserver import Server, Request, Response
8+
9+
10+
pool = socket
11+
server = Server(pool, "/static", debug=True)
12+
13+
14+
@server.route("/")
15+
def base(request: Request):
16+
"""
17+
Serve a default static plain text message.
18+
"""
19+
return Response(request, "Hello from the CircuitPython HTTP Server!")
20+
21+
22+
server.serve_forever("0.0.0.0")

0 commit comments

Comments
 (0)