This is the documentation for uWeb. Click here if you're looking for the docs for uWeb-uasyncio
- Table of Contents
- Objects
- Attributes
- Methods
uWeb.routes(routes={})
- Description - ParametersuWeb.start(log=True)
- Description - ParametersuWeb.render(html_file, layout='layout.html', variables=False, status=OK)
- Description - ParametersuWeb.sendJSON(dict_to_send={})
- Description - ParametersuWeb.sendFile(filename)
- Description - ParametersuWeb.sendStatus(status_code)
- Description - ParametersuWeb.sendHeaders(headers_dict={})
- Description - ParametersuWeb.sendBody(body_content)
- Description - ParametersuWeb.setSupportedFileTypes(file_types = ['js', 'css'])
- Description - Parameters- Helpers
uWeb.router()
- DescriptionuWeb.readFile(file)
- Description - Parameters - ReturnsuWeb.send(content)
- Description - ParametersuWeb.processRequest()
- DescriptionuWeb.resolveRequestLine()
- Description - ReturnsloadJSON(string)
- Description - Parameters- Constants - HTTP Methods - HTTP Status Codes - MIME types
Initialize a uWeb object by configuring socket to bind and listen to specified address and port.
- address - (str) address to listen on
- port - (int) port to listen on
uWeb.version
- (str) uWeb versionuWeb.address
- address that is bound touWeb.port
- (int) port that is bound touWeb.routes_dict
- (dict) all routesuWeb.request_command
- (str) HTTP method of request(ie: POST, GET, PUT, etc)uWeb.request_path
- (str) requested pathuWeb.request_http_ver
- (str) HTTP version of requestuWeb.request_body
- (str) body of current requestuWeb.request_headers
- (dict) headers of current requestuWeb.client_socket
- (socket) socket object of active socketuWeb.client_address
- address of client
Use this method to specify routes for the app.
-
routes - (dict) dictionary containing routes in format:
{ (HTTP_METHOD, PATH): ACTION, (HTTP_METHOD, PATH): ACTION }
- HTTP_METHOD - method to listen for(see Constants)
- PATH - URL to listen for
- ACTION - function to be run when route matches
Example:
{ (uWeb.GET, "/"): home, (uWeb.POST, "/post"): post, (uWeb.GET, "/json"): jsonn, (uWeb.GET, "/header"): header }
Start the server.
- log - (bool) default: True; toggle logging of client information and requests to console
Send HTML file to client's browser
-
html_file - (str) file name of html file to render
-
layout - (str) layout to render
html_file
with(see Layout Rendering) -
variables - (dict) dictionary of variables to render html with(see Template Rendering).
Example:
{ "variable_name_in_html": "replace_with_this", "another_one", (1 + 1) }
-
status - (str) HTTP status to send to client. Default: uWeb.OK(see Constants)
Send JSON body to client.
- dict_to_send - (dict) dictionary with JSON data
- Send file to client along with its appropriate Content-Type header. This is automatically called depending on the path of the HTTP request. EX: if a .js is requested, uWeb will look for it and send it if it exists.
- Add MIME types by appending to the
MIME_TYPES
dictionary
- filename - (str) name of file to send
Send HTTP response header to client with specified status
- status_code - (str) HTTP status code(see Constants)
Send HTTP headers to client.
- headers_dict - (dict) dictionary containing header and values to be sent to client. Example:
{
'header1': 'one',
'header2': 'two',
'header3': 'three',
}
Send response body content to client
- body_content - (bytestring) body content to send
- Specify the file extensions to be allowed to be sent to the client if it is requested. Use to protect your backend of your Microcontroller.
- When allowing additional files, don't forget to include 'js' and 'css' in the list as well
- This only applies to GET requests. You can still manually send files of any extension with sendFile()
- NOTE: Be careful when allowing file types such as .py because the client can request /boot.py and may exposed sensitive info such as your wi-fi password if you have it set there.
- file_types - (list) file extensions to whitelist. Default: .js and .css
Handles requests and run actions when a route matches
Read and encode a file. Depending on your hardware, this method may raise a memory allocation error if the file is larger than the available memory.
- file - (str) filename of file to be read and encoded
- (bytestring) encoded file
Basic method to send bytestring to client
- content - (bytestring) content to send
Process request from client by extracting headers to request_headers and extract body to request_body if it is a POST request.
Parse request line from client. Sets: request_command, request_path, and request_http_ver
- (bool) True: if a valid request_line; False: if request_line empty
Not part of uWeb class. Easy way to convert a request_body containing a JSON string to a dict
-
string - (str) JSON string to convert to dictionary
-
(dict) dictionary with converted JSON
- uWeb.GET = 'GET'
- uWeb.POST = 'POST'
- uWeb.PUT = 'PUT'
- uWeb.DELETE = 'DELETE'
- uWeb.OK = b"200 OK"
- uWeb.NOT_FOUND = b"404 Not Found"
- uWeb.FOUND = b"302 Found"
- uWeb.FORBIDDEN = b"403 Forbidden"
- uWeb.BAD_REQUEST = b"400 Bad Request"
- uWeb.ERROR = b"500 Internal Server Error"
- 'css': 'text/css'
- 'html': 'text/html'
- 'jpeg': 'image/jpeg'
- 'jpg': 'image/jpeg'
- 'js': 'text/javascript'
- 'json': 'application/json'
- 'rtf': 'application/rtf'
- 'svg': 'image/svg+xml'