lightweight asynchronous easy-to-use web-framework for micropython
It's really simple, just a fast example:
import deaweb
app = deaweb.Server()
@app.handler('/hi')
def hello_world_handler(request):
return 'Hello World!'
For more details look at API reference
You can use upip to install deaweb. Example:
import upip
upip.install('deaweb')
import deaweb
Just download deaweb.mpy
or deaweb.py
file and put it near your app to use .py/.mpy file as python module.
After just import deaweb in your app:
import deaweb
Also you can complile deaweb.mpy file from deaweb.py using MicroPython cross compiler
MemoryError: memory allocation failed, allocating 72 bytes
In this case just use deaweb.mpy file
Contains dict of a request headers. None if headers not read. Example:
@app.handler('/')
def test(request):
# print's Content-Type of request or None if not provided
print(request.headers.get('Content-Type'))
Contains string with a request method (like a GET/POST/PUT... etc). None if headers not read.
Contains string with a request path (like a /get, /hi or /). None if headers not read.
Contains string of a request protocol. None if headers not read.
Returns value of params provided in query string. Needs to be headers read. Returns None if parameter not found/provided. Return list if several parameter with same name provided. Example:
@app.handler('/get_data')
def data_handler(request):
# if user sent request http://ip:port/get_data?key=userdata&a=1&a=2 will print
print(request.get('param1')) # None
print(request.get('key')) # userdata
print(request.get('a')) # ['1', '2']
Return request Content-Length header value. If header not provided returns 0.
Reads payload and storing it into file_path Example:
@app.handler('/put_data')
def put_data_handler(request):
await request.readinto('some_file.ext')
return 'ok'
Same with Request.readinto, but If you got MemoryError then just False will returned. If not True. And file first will be written into buffer file and if download successful renamed to yours file name
Returns request reader
@app.handler('/upload_file')
def upload_file_handler(request):
with open('file.tmp', 'w') as f:
content = await request.reader.read()
f.write(content)
Returns request writer Example:
@app.handler('/')
def main_handler(request):
response = Response('My response', request=request) # creating response object
await response.awrite() # writing response
You can do not use this class to return a response
Just return string what you want from handler to make a response
body
(str) - Response string. By default None.
status_code
(int) - Response status code. By default 200.
content_type
(str) - Response Content-Type. By default 'text/html'.
Use this method to write Response.body
to the client.
Example:
await response.awrite()
or
yield from response.awrite()
Use this method to close response/communication with client.
Respose body. None if not set.
Respose status code. By default 200.
Respose Content-Type header value. 'text/html' if not set.
Respose Content-Length header value (body attribute length). 0 if no body provided.
Used to return file as response
TODO:
- file name in header
- file content type in depend on file extension
- read N bytes instead readline Example:
@app.handler('/get_some_file')
def get_some_file_handler(request):
return FileResponse('some_file.ext')