MicroWebSrv2
The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
Install / Use
/learn @jczic/MicroWebSrv2README
MicroWebSrv2 is the new powerful embedded Web Server for MicroPython and CPython that supports route handlers, modules like WebSockets or PyhtmlTemplate and a lot of simultaneous requests (in thousands!).
Fully asynchronous, its connections and memory management are very optimized and truly fast.
Mostly used on Pycom WiPy, ESP32, STM32 on Pyboard, ... Robust and efficient! (see Features)
:small_orange_diamond: Download the latest version (Zip)
<br /> _ __ __ _ __ ____
_ __ ___ (_) ___ _ __ ___/ / /\ \ \___| |__ / _\_ ____ _|___ \
| '_ ` _ \| |/ __| '__/ _ \ \/ \/ / _ | '_ \\ \| '__\ \ / / __) |
| | | | | | | (__| | | (_) \ /\ | __| |_) _\ | | \ V / / __/
|_| |_| |_|_|\___|_| \___/ \/ \/ \___|_.__/\__|_| \_/ |_____| JC`zic & HC²
<br />
:bookmark_tabs: Table of contents
- About
- Features
- Install
- Demo
- Usage
- Documentation
- Author
- License
<a name="about"></a>
:anger: About
This project follows the embedded MicroWebSrv, which is mainly used on microcontrollers such as Pycom, ESP32 and STM32 on Pyboards.
In a need for scalability and to meet the IoT universe, microWebSrv2 was developed as a new project and has been completely redesigned to be much more robust and efficient that its predecessor.
Internal mechanisms works directly at I/O level, are fully asynchronous from end to end, and manages the memory in a highly optimized way.
Also, architecture makes its integration very easy and the source code, MIT licensed, remains really small.
<a name="features"></a>
:rocket: Features
-
Embed microWebSrv2 into your microcontrollers as a powerful web server.
-
Benefit from a fully asynchronous architecture that allows to process many concurrent requests very quickly.
-
Use multiple worker threads to parallelize simultaneous processes.
-
Adjust settings to fine-tune resources usage and sizing pre-allocated memory.
-
Load additional modules to extend the server's functionalities.
-
Customize the management of centralized logs.
-
Apply SSL/TLS security layer and certificate on web connections (https mode).
-
Define web routes with variable arguments in order to be able to process the targeted requests.
-
Receive any type of request such as
GET,HEAD,POST,PUT,DELETE,OPTIONS,PATCH, ... -
Use the route resolver (from the path) and the path builder (from the route) for convenience.
-
Increase loading speed by automatically allowing web clients to cache static files.
-
Receive name/value pairs from URL encoded forms.
-
Send and receive JSON objects and use them to create a RESTful API style.
-
Play with AjAX requests to interact quickly with a web application.
-
Define the origin of resources and allow all values of CORS pre-flight requests.
-
Verify that a request is successfully authenticated by the Basic or Bearer method.
-
Reduce the number of persistent connections per web client with keep-alive mode support.
-
Respond to a request by using a data stream as content, sent with known length or in chunked transfer-encoding.
-
Use a file to respond to a request that will be treated as on-the-fly content or as an attachment to download.
-
Take advantage of the WebSockets module to exchange messages in real time via WS or secured WSS connection.
-
Create .pyhtml pages for an HTML rendering with integrated Python using the PyhtmlTemplate module.
<a name="install"></a>
:electric_plug: Install
- Solution 1 Run:
pip3 install --user git+https://github.com/jczic/MicroWebSrv2.git#egg=MicroWebSrv2
- Solution 2, clone the GitHub repository from the terminal:
git clone https://github.com/jczic/MicroWebSrv2.git
and run:
cd MicroWebSrv2 && pip install --user .
- Solution 3, download the ZIP file and extract it to a folder of your choice.
<a name="demo"></a>
:vertical_traffic_light: Demo
-
Start the example:
> python3 main.py -
Open your web browser at:
- http://localhost to view the main page
- http://localhost/test-redir to test a redirection
- http://localhost/test-post to test a POST form
- http://localhost/wstest.html to test the WebSockets page
- http://localhost/wschat.html to test the multi-users chat page
- http://localhost/test.pyhtml to test a pyhtml template page

<a name="usage"></a>
:gear: Usage
from MicroWebSrv2 import *
from time import sleep
mws2 = MicroWebSrv2()
mws2.StartManaged()
# Main program loop until keyboard interrupt,
try :
while True :
sleep(1)
except KeyboardInterrupt :
mws2.Stop()
<a name="documentation"></a>
:books: Documentation
<a name="mws2-package"></a>
-
MicroWebSrv2 package
The Python package comes with the following files:
- :small_red_triangle_down: /MicroWebSrv2
- :small_orange_diamond: __init__.py
- :small_orange_diamond: microWebSrv2.py
- :small_orange_diamond: webRoute.py
- :small_orange_diamond: httpRequest.py
- :small_orange_diamond: httpResponse.py
- :small_blue_diamond: /libs
- :small_orange_diamond: XAsyncSockets.py
- :small_orange_diamond: urlUtils.py
- :small_blue_diamond: /mods
- :small_orange_diamond: WebSockets.py
- :small_orange_diamond: PyhtmlTemplate.py
- :small_red_triangle_down: /MicroWebSrv2
<a name="working-with-mws2"></a>
-
Working with microWebSrv2
To work with microWebSrv2, you must first import the package as follows:
from MicroWebSrv2 import *There are 5 main elements with which you will work:
- Web server (see MicroWebSrv2 class)
- Routes (see Web routes)
- Requests (see HttpRequest class)
- Responses (see HttpResponse class)
- Modules (see Additional modules)
Now, you have everything you need to continue :sunglasses:
<a name="async-logic"></a>
-
Asynchronous logic
microWebSrv2 is based on a fully asynchronous I/Os logic.
This means that many requests and responses can be processed concurrently, allowing other necessary processing at any time within a single thread.
In addition, it is possible to use several shared workers in order to be able to parallelize processes that could be blocking.
Finally, memory buffers required for data processing are pre-allocated as best as possible to maximize performance and prevent excessive memory requirements.
Thus, it is possible to maintain several thousand persistent connections but also, to benefit from a low consumption of resources on embedded platforms.
microWebSrv2 leans on the XAsyncSockets layer.
<a name="config-web-sr
