# micropython-uaioweb
A very minimal asyncio web framework for MicroPython. Doesn't come with all the bells and whistles you might want out of a serious web framework but the goal is just to make asyncio HTTP applications in MicroPython as simple and efficient as possible.
## Current features
* minimal overhead in terms of code size or memory use
* easy integration into existing asyncio projects by running as a normal task alongside others
* basic endpoint/method based routing similar to flask (currently doesn't do any pattern matching)
* parses http request line, headers, and query strings
* supports WebSockets!
* supports Server-Sent Events!
## Rationale
[`micropython-uaioweb`](https://github.com/damiencorpataux/micropython-uaioweb) is a packaged version of [`micropython-aioweb`](https://github.com/wybiral/micropython-aioweb), an amazing minimal and functional HTTP toolkit for micropython by Davy Wybiral.
This project aims at releasing the library on [PyPI](https://pypi.org/project/micropython-uaioweb/) by forking and reworking branch `main` with 1 commit per version.
## Examples
### Basic "Hello world!"
```python
import uaioweb
from uaioweb import asyncio # Note: compatibility between micropython and python.
app = uaioweb.App(host='0.0.0.0', port=80)
# root route handler
@app.route('/')
async def handler(r, w):
# write http headers
w.write(b'HTTP/1.0 200 OK\r\n')
w.write(b'Content-Type: text/html; charset=utf-8\r\n')
w.write(b'\r\n')
# write page body
w.write(b'Hello world!')
# drain stream buffer
await w.drain()
# Start event loop and create server task
loop = asyncio.get_event_loop()
loop.create_task(app.serve())
loop.run_forever()
```
### POST request handler
```python
@app.route('/', methods=['POST'])
async def handler(r, w):
body = await r.read(1024)
form = uaioweb.parse_qs(body.decode())
name = form.get('name', 'world')
# write http headers
w.write(b'HTTP/1.0 200 OK\r\n')
w.write(b'Content-Type: text/html; charset=utf-8\r\n')
w.write(b'\r\n')
# write page body
w.write(b'Hello {}!'.format(name))
# drain stream buffer
await w.drain()
```
### WebSocket handler
```python
# /ws WebSocket route handler
@app.route('/ws')
async def ws_handler(r, w):
# upgrade connection to WebSocket
ws = await WebSocket.upgrade(r, w)
while True:
evt = await ws.recv()
if evt is None or evt['type'] == 'close':
# handle closed stream/close event
break
elif evt['type'] == 'text':
# print received messages and echo them
print('Received:', evt['data'])
await ws.send(evt['data'])
```
### SSE (Server-Sent Events) handler
```python
# /events EventSource route handler
@app.route('/events')
async def ws_handler(r, w):
# upgrade connection to text/event-stream
sse = await uaioweb.EventSource.upgrade(r, w)
count = 0
while True:
count += 1
try:
await sse.send('Hello world #{}'.format(count))
except:
break
await asyncio.sleep(1)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/damiencorpataux/micropython-uaioweb",
"name": "micropython-uaioweb",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "micropython,python,web,server,http,websocket,asyncio,uasyncio,lightweight,simple,minimalistic",
"author": "Davy Wybiral, Damien Corpataux (fork)",
"author_email": "not-disclosed@example.com, d@mien.ch",
"download_url": "https://files.pythonhosted.org/packages/e4/e0/c3d6a06b544b0b1c612c43c51a3e829c9071d4fe8e52fadf30383d06aa67/micropython-uaioweb-0.0.3.tar.gz",
"platform": null,
"description": "# micropython-uaioweb\nA very minimal asyncio web framework for MicroPython. Doesn't come with all the bells and whistles you might want out of a serious web framework but the goal is just to make asyncio HTTP applications in MicroPython as simple and efficient as possible.\n\n## Current features\n* minimal overhead in terms of code size or memory use\n* easy integration into existing asyncio projects by running as a normal task alongside others\n* basic endpoint/method based routing similar to flask (currently doesn't do any pattern matching)\n* parses http request line, headers, and query strings\n* supports WebSockets!\n* supports Server-Sent Events!\n\n## Rationale\n[`micropython-uaioweb`](https://github.com/damiencorpataux/micropython-uaioweb) is a packaged version of [`micropython-aioweb`](https://github.com/wybiral/micropython-aioweb), an amazing minimal and functional HTTP toolkit for micropython by Davy Wybiral.\n\nThis project aims at releasing the library on [PyPI](https://pypi.org/project/micropython-uaioweb/) by forking and reworking branch `main` with 1 commit per version.\n\n## Examples\n### Basic \"Hello world!\"\n```python\nimport uaioweb\nfrom uaioweb import asyncio # Note: compatibility between micropython and python.\n\napp = uaioweb.App(host='0.0.0.0', port=80)\n\n# root route handler\n@app.route('/')\nasync def handler(r, w):\n # write http headers\n w.write(b'HTTP/1.0 200 OK\\r\\n')\n w.write(b'Content-Type: text/html; charset=utf-8\\r\\n')\n w.write(b'\\r\\n')\n # write page body\n w.write(b'Hello world!')\n # drain stream buffer\n await w.drain()\n\n# Start event loop and create server task\nloop = asyncio.get_event_loop()\nloop.create_task(app.serve())\nloop.run_forever()\n```\n### POST request handler\n```python\n@app.route('/', methods=['POST'])\nasync def handler(r, w):\n body = await r.read(1024)\n form = uaioweb.parse_qs(body.decode())\n name = form.get('name', 'world')\n # write http headers\n w.write(b'HTTP/1.0 200 OK\\r\\n')\n w.write(b'Content-Type: text/html; charset=utf-8\\r\\n')\n w.write(b'\\r\\n')\n # write page body\n w.write(b'Hello {}!'.format(name))\n # drain stream buffer\n await w.drain()\n```\n### WebSocket handler\n```python\n# /ws WebSocket route handler\n@app.route('/ws')\nasync def ws_handler(r, w):\n # upgrade connection to WebSocket\n ws = await WebSocket.upgrade(r, w)\n while True:\n evt = await ws.recv()\n if evt is None or evt['type'] == 'close':\n # handle closed stream/close event\n break\n elif evt['type'] == 'text':\n # print received messages and echo them\n print('Received:', evt['data'])\n await ws.send(evt['data'])\n```\n### SSE (Server-Sent Events) handler\n```python\n# /events EventSource route handler\n@app.route('/events')\nasync def ws_handler(r, w):\n # upgrade connection to text/event-stream\n sse = await uaioweb.EventSource.upgrade(r, w)\n count = 0\n while True:\n count += 1\n try:\n await sse.send('Hello world #{}'.format(count))\n except:\n break\n await asyncio.sleep(1)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Minimal asyncio web server for HTTP and Websocket.",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/damiencorpataux/micropython-uaioweb"
},
"split_keywords": [
"micropython",
"python",
"web",
"server",
"http",
"websocket",
"asyncio",
"uasyncio",
"lightweight",
"simple",
"minimalistic"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "becf25e30883c1c480a9fe6d36dfdd5505032ce2d21ca34b16f3b54e22edf03c",
"md5": "b7de6ba4c61dc2506648d84d23b487d0",
"sha256": "9bd7f37b15eae0cd4065108b9701dedf7dd6849c25184836b96606197720f9c1"
},
"downloads": -1,
"filename": "micropython_uaioweb-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b7de6ba4c61dc2506648d84d23b487d0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5808,
"upload_time": "2023-06-25T12:04:22",
"upload_time_iso_8601": "2023-06-25T12:04:22.975308Z",
"url": "https://files.pythonhosted.org/packages/be/cf/25e30883c1c480a9fe6d36dfdd5505032ce2d21ca34b16f3b54e22edf03c/micropython_uaioweb-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e4e0c3d6a06b544b0b1c612c43c51a3e829c9071d4fe8e52fadf30383d06aa67",
"md5": "f894344356d47154f770d1c2eb9e176e",
"sha256": "edaac99ea778775f372a9c44747d7e958d3843f37d468136f2e1defd7a8d3f7a"
},
"downloads": -1,
"filename": "micropython-uaioweb-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "f894344356d47154f770d1c2eb9e176e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5252,
"upload_time": "2023-06-25T12:04:24",
"upload_time_iso_8601": "2023-06-25T12:04:24.514539Z",
"url": "https://files.pythonhosted.org/packages/e4/e0/c3d6a06b544b0b1c612c43c51a3e829c9071d4fe8e52fadf30383d06aa67/micropython-uaioweb-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-25 12:04:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "damiencorpataux",
"github_project": "micropython-uaioweb",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "micropython-uaioweb"
}