Skip to content

add cpython example #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/httpserver_cpython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2024 Ted Timmons
#
# SPDX-License-Identifier: Unlicense
#
# Simple example showing how to use httpserver in cpython (e.g., on a computer).

import socket
from adafruit_httpserver import Server, Request, Response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every other example has a blank line between general imports and adafruit_httpserver imports



# fake the wifi class, reduces changes required later in the code.
class wifi: # pylint: disable=too-few-public-methods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary to create such class, I believe simply passing "0.0.0.0" as argument to server.serve_forever() is enough. The only reason other examples use wifi.radio.ipv4_address is because this is the way CircuitPython handles that.

class radio: # pylint: disable=too-few-public-methods
ipv4_address = "0.0.0.0"


pool = socket
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))