MojoScale Studio Docs
API Reference

webserver

Embedded webserver native Berry module.

Studio Docs Networking

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.

API 10 available

webserver.start(port: int = None) -> any

Start the embedded webserver.

Parameters
Name Type Pass as Required Description
port int positional or keyword No Optional HTTP port, defaulting to firmware configuration.
Returns

any Native start result.

webserver.stop() -> any

Stop the embedded webserver.

Returns

any Native stop result.

webserver.route(path: str, method: any, callback: callback) -> str

Register a route handler.

Parameters
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.
Returns

str Native registration result.

webserver.unroute(path: str, method: any) -> any

Remove a route handler.

Parameters
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.
Returns

any Native removal result.

webserver.respond(body: any, content_type: str = None, status: any = None) -> any

Respond to the active request.

Parameters
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.
Returns

any Native response result.

webserver.send_text(text: str, status: any = None) -> any

Send a plain text response to the active request.

Parameters
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.
Returns

any Native response result.

webserver.stats() -> dict

Return webserver statistics.

Returns

dict Native map-like value with route and request status.

webserver.set_token(token: any) -> any

Enable bearer-token style authentication.

Parameters
Name Type Pass as Required Description
token any positional or keyword Yes Token required by incoming requests.
Returns

any Native auth result.

webserver.set_basic_auth(username: str, password: any) -> any

Enable HTTP basic authentication.

Parameters
Name Type Pass as Required Description
username str positional or keyword Yes Basic auth username.
password any positional or keyword Yes Basic auth password.
Returns

any Native auth result.

webserver.clear_auth() -> any

Disable webserver authentication.

Returns

any Native auth result.