MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
jczic
Posts: 11
Joined: Fri Sep 01, 2017 2:10 am
Location: Paris, France
Contact:

MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by jczic » Tue Oct 22, 2019 3:06 pm

Hello all!

I propose you my new powerful embedded Web Server for MicroPython (and CPython) that supports route handlers, modules like WebSockets or PyhtmlTemplate and a lot of simultaneous requests.

Mostly used on ESP32, Pycom WiPy, STM32 on Pyboard, ... Robust and efficient (thousands requests on CPython!)

--> https://github.com/jczic/MicroWebSrv2
GitHub - Open Source - MIT - Great documentation ;)

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.
  • 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.
Image

andrewleech
Posts: 2
Joined: Tue Mar 27, 2018 12:25 am

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by andrewleech » Tue Oct 22, 2019 11:38 pm

Brilliant work, that's an amazingly featured server for the platform, thanks!

On a side note, at work I've been using a (different) micropython web server attached to the serial port instead of a socket (either the normal serial with repl disabled via dupterm, or a second usb virtual com port on stm32).
Then on computer using either a simple serial-tcp proxy whipped up in python, or using a wrapper around pyserial to make it look like a socket that's then passed into a class based on the builtin HTTPConnection.
It works surprisingly well as a way of communicating with a micropython board that doesn't have any real networking.

It was already on my backlog to making an example of this to share based on MicroWebSrv, now I guess I'll want to target MicroWebSrv2 instead!

nevercast
Posts: 4
Joined: Tue Oct 22, 2019 11:37 pm

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by nevercast » Tue Oct 22, 2019 11:42 pm

Hi jczic!

I've used your original version of MicroWebSrv as well as your MicroDNSSrv and MicroWiFi libraries and they were really enjoyable to work with! Looking forward to using MicroWebSrv2 in the near future! Thank you for posting.

User avatar
jimmo
Posts: 878
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by jimmo » Tue Oct 22, 2019 11:46 pm

hey jczic, thank you!! This is really great, and I'm sure many people will find it really useful. :D
jczic wrote:
Tue Oct 22, 2019 3:06 pm
Benefit from a fully asynchronous architecture that allows to process many concurrent requests very quickly.
I notice that this requires the use of _thread, which (at the current time) is only enabled on ESP32.

I'm not 100% sure whether supporting threads by default in release firmware is likely to happen. At the very least though, the micropython.org/download page should be updated to add threaded builds for PYBD firmware (to go with the threaded builds of pyb1.x already provided). I'll raise a bug.

Either way though, would it be possible to add something to the MicroWebSrv2 docs about needing to use firmware with threading enabled?

Out of curiosity, is it feasible to make a non-threaded fallback for XAsyncSockets? I suspect this might be hard to do unless you have two threads, unless you can take advantage of the (undocumented...!) `setsockopt(socket.SOL_SOCKET, 20, accept_handler)` that webrepl.py uses.

User avatar
marfis
Posts: 193
Joined: Fri Oct 31, 2014 10:29 am
Location: Zurich / Switzerland

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by marfis » Wed Oct 23, 2019 7:07 pm

Great work, thanks!
should be updated to add threaded builds for PYBD firmware (to go with the threaded builds of pyb1.x already provided)
+1.

I have used microwebsrv for mozilla iot webthings https://github.com/mozilla-iot/webthing-upy and found it difficult to get started, especially because it required the thread module and prebuilds for the pyboard d were not available.

It would be nice to see if microwebsrv2 is compatible with mozilla iot.

User avatar
dhylands
Posts: 3246
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by dhylands » Wed Oct 23, 2019 7:12 pm

If it supports websockets then it should work fine with Mozilla IoT.

User avatar
jczic
Posts: 11
Joined: Fri Sep 01, 2017 2:10 am
Location: Paris, France
Contact:

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by jczic » Wed Nov 13, 2019 2:44 am

Thank you all for your messages! ;)
Indeed, MicroWebSrv2 requires that "_thread" be present in firmware.
However, it is able to run directly, in the current thread (it is not recommended).
Its fast operating mode is to use it with only a single dedicated thread but several are also possible (rather for computer servers with CPython).
Did you get a chance to test it? :)

User avatar
mattyt
Posts: 249
Joined: Mon Jan 23, 2017 6:39 am

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by mattyt » Wed Nov 13, 2019 5:22 am

Hiya jczic,

Just a quick note to say that I shared the MicroWebSrv2 announcement at the last Melbourne MicroPython Meetup (I haven't yet added a blog post for that meetup but I will!) and to the recent Adafruit Python on Hardware Newsletter.

We've used it briefly at work for an internal tool and it worked exactly as advertised. I'm hoping to use it more in the near-future...

Thanks for your work on the library! :)

User avatar
Hyperion
Posts: 6
Joined: Wed Oct 16, 2019 9:34 am

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by Hyperion » Wed Nov 20, 2019 12:37 pm

Can't get any luck getting this to run on my ESP32. After running the import command, I get a MemoryError. How do you import it with only around 80kB RAM left?

Code: Select all

>>> import MicroWebSrv2

       ---------------------------
       - Python pkg MicroWebSrv2 -
       -      version 2.0.3      -
       -     by JC`zic & HC2     -
       ---------------------------

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MicroWebSrv2/__init__.py", line 18, in <module>
  File "MicroWebSrv2/microWebSrv2.py", line 8, in <module>
  File "MicroWebSrv2/httpRequest.py", line 8, in <module>
MemoryError: memory allocation failed, allocating 136 bytes
>>> 
Last edited by Hyperion on Wed Nov 20, 2019 3:43 pm, edited 1 time in total.

User avatar
dhylands
Posts: 3246
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: MicroWebSrv2, new asynchronous Web server for MicroPython (+Routes +WebSockets +Template engine).

Post by dhylands » Wed Nov 20, 2019 3:36 pm

WHen I used it, I hd to make sure it was used as a frozen module.

Post Reply