super simple web server for wifi-based micro-controllers running MicroPython
uWeb
v1.1 - 5/19/20
v1.0 - 7/6/19
uWeb-uasyncio
v1.0 - 5/20/20
MicroPython Libraries:
This repo contains both uWeb and uWeb-uasyncio. Both versions have their own use cases
;-; | μWeb | μWeb-uasyncio |
---|---|---|
concurrency | synchronous, uses usocket(blocking sockets) | asynchronous, uses uasyncio(non-blocking sockets) |
when to use | when you need a simple web server to serve HTML or send data | when endpoints call functions that take time to run and having the server still be able to respond to requests is important |
use cases | simple API that serves static files or sends some JSON data(concurrency not required) | app where a request to an endpoint invokes a function that sleeps for some time but still needs to respond to incoming requests |
TLDR; uWeb can be used when concurrency is not important. uWeb-uasyncio can be used when concurrency is important
uWeb.py
or uWeb_uasyncio.py
depending on your needsNo MicroPython board? No problem!
You can run uWeb with the MicroPython unix port
micropython uWeb_example.py
or micropython uWeb_uasyncio_example.py
in your console.from uWeb import uWeb, loadJSON
server = uWeb("0.0.0.0", 8000) #init uWeb object
def home(): #render HTML page
vars = {
'name': 'MicroPython',
'answer': (1+1)
}
server.render('content.html', variables=vars)
def header(): #send headers to client
server.sendStatus(server.OK)
server.sendHeaders({
'header1': 'one',
'header2': 'two',
'header3': 'three',
})
def post(): #print JSON body from client
print(loadJSON(server.request_body).items())
def jsonn(): #send JSON to client
server.sendJSON({'status':'okkk'})
#configure routes
server.routes(({
(uWeb.GET, "/"): home,
(uWeb.POST, "/post"): post,
(uWeb.GET, "/json"): jsonn,
(uWeb.GET, "/header"): header
}))
#start server
server.start()
import uasyncio
from uWeb_uasyncio import uWeb_uasyncio as uWeb
from uWeb_uasyncio import loadJSON
server = uWeb("0.0.0.0", 8000) #init uWeb object
def home(): #render HTML page
vars = {
'name': 'MicroPython',
'answer': (1+1)
}
await server.render('content.html', variables=vars)
def printTen(): # counts to ten while still accepting incoming requests
await server.sendStatus(server.OK)
for i in range(10):
print(i)
await uasyncio.sleep(1)
def header(): #send headers to client
await server.sendStatus(server.OK)
await server.sendHeaders({
'header1': 'one',
'header2': 'two',
'header3': 'three',
})
def post(): #print JSON body from client
print('Payload: ', loadJSON(server.request_body))
await uasyncio.sleep(0)
def jsonn(): #send JSON to client
await server.sendJSON({'status':'okkk'})
#configure routes
server.routes(({
(uWeb.GET, "/"): home,
(uWeb.GET, "/ten"): printTen,
(uWeb.POST, "/post"): post,
(uWeb.GET, "/json"): jsonn,
(uWeb.GET, "/header"): header
}))
#start server
server.start()
await
statement in its bodyawait
await
able, await uasyncio.sleep(0)
can be used (as seen in the example)uasyncio
: uasyncio docsExample:
content.html
<h1>Hello from !</h1>
<h3>1 + 1 = </h3>
example.py
vars = {
'name': 'MicroPython',
'answer': (1+1)
}
server.render('content.html', variables=vars)
will render as:
<h1>Hello from MicroPython!</h1>
<h3>1 + 1 = 2</h3>
layout.html
layout
argument in the render
functionUsing the example from above:
content.html
<h1>Hello from !</h1>
<h3>1 + 1 = </h3>
cool-layout.html
<head>
<title>cool site</title>
</head>
<body>
<h1>this is a cool layout dude</h1>
</body>
example.py
vars = {
'name': 'MicroPython',
'answer': (1+1)
}
server.render('content.html', layout='cool-layout.html', variables=vars) # if you are using layout.html, the layout argument may be left out
will render as:
<head>
<title>cool site</title>
</head>
<body>
<h1>this is a cool layout dude</h1>
<h1>Hello from MicroPython!</h1>
<h3>1 + 1 = 2</h3>
</body>