import webserver
The webserver module exposes a tiny firmware HTTP server for local routes, responses, and simple authentication. Use it for device setup screens, local status endpoints, and diagnostics.
Quick example
import webserver
def handle_home(path):
html = "<html><body><h1>MojoScale</h1><p>Device online.</p></body></html>"
webserver.respond(html, "text/html", 200)
webserver.route("/", "GET", handle_home)
webserver.start(80)
importwebserver
Embedded webserver native Berry module.
The webserver module exposes a tiny firmware HTTP server for local routes, responses, and simple authentication. Use it for device setup screens, local status endpoints, and diagnostics.
webserver.start(port: int = None) -> any
webserver.start(port: int = None) -> anyStart the embedded webserver.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
port |
int |
positional or keyword | No | Optional HTTP port, defaulting to firmware configuration. |
any Native start result.
webserver.stop() -> any
webserver.stop() -> anyStop the embedded webserver.
any Native stop result.
webserver.route(path: str, method: any, callback: callback) -> str
webserver.route(path: str, method: any, callback: callback) -> strRegister a route handler.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | URL path, such as "/status". |
method |
any |
positional or keyword | Yes | HTTP method, such as "GET" or "POST". |
callback |
callback |
positional or keyword | Yes | Named route handler function. Pass the function without quotes; Studio sends its name string to firmware. For GET and DELETE requests, the callback receives path. For POST and PUT requests, the callback receives path and body. |
str Native registration result.
webserver.unroute(path: str, method: any) -> any
webserver.unroute(path: str, method: any) -> anyRemove a route handler.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | URL path to remove. |
method |
any |
positional or keyword | Yes | HTTP method for the route. |
any Native removal result.
webserver.respond(body: any, content_type: str = None, status: any = None) -> any
webserver.respond(body: any, content_type: str = None, status: any = None) -> anyRespond to the active request.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
body |
any |
positional or keyword | Yes | Response body. |
content_type |
str |
positional or keyword | No | Optional MIME type. |
status |
any |
positional or keyword | No | Optional HTTP status code. |
any Native response result.
webserver.send_text(text: str, status: any = None) -> any
webserver.send_text(text: str, status: any = None) -> anySend a plain text response to the active request.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
text |
str |
positional or keyword | Yes | Response body text. |
status |
any |
positional or keyword | No | Optional HTTP status code. |
any Native response result.
webserver.stats() -> dict
webserver.stats() -> dictReturn webserver statistics.
dict Native map-like value with route and request status.
webserver.set_token(token: any) -> any
webserver.set_token(token: any) -> anyEnable bearer-token style authentication.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
token |
any |
positional or keyword | Yes | Token required by incoming requests. |
any Native auth result.
webserver.set_basic_auth(username: str, password: any) -> any
webserver.set_basic_auth(username: str, password: any) -> anyEnable HTTP basic authentication.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
username |
str |
positional or keyword | Yes | Basic auth username. |
password |
any |
positional or keyword | Yes | Basic auth password. |
any Native auth result.
webserver.clear_auth() -> any
webserver.clear_auth() -> anyDisable webserver authentication.
any Native auth result.