uasyncio.open_connection and ssl

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.
Post Reply
leosok
Posts: 18
Joined: Sun May 02, 2021 10:10 pm

uasyncio.open_connection and ssl

Post by leosok » Sun May 02, 2021 10:58 pm

tl;dr: How to use this version of uasyncio? with micopython 1.14 on ESP32

Hello,
I need to POST a big chunk of data to a https-endpoint. (Send an email via Mailgun). I did try a lot of things (using SMTP etc.) but my ESP32 never has enough memory when the requests get a bit bigger. Smaller requests work well with urequests.post().
My newest idea is to POST with HTTP chuncked transfer, and to send my data line by line directly from a file on the flash. Like this memory usage should be minimal. I was able to update this async-http-client (uaiohttpclient) to create a chunked Post request. Unfortunatly it is not possible to send it over SSL, which I need.

The relevant code in uaiohttpclient is:

Code: Select all

reader, writer = yield from asyncio.open_connection(host, 80)
which opens a socket and returns a non-blocking StreamReader/Writer.
I changed the code to:

Code: Select all

reader, writer = yield from asyncio.open_connection(host=host, port=port, ssl=is_ssl)
which fails, because the open_connection method of the uasyncio version included in micropython 1.14 has no ssl-support. I found an uasyncio version in pycopy, which does support ssl here. I would like to use it to solve my problem. Just importing this file was not possible. I guess I have to somehow patch the whole asyncio library, which is included in the firmware-binary.
Any idea where to go from here would be appreciated.
Thanks,
Leo

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio.open_connection and ssl

Post by pythoncoder » Mon May 03, 2021 7:48 am

This forum is for official MicroPython so I doubt many people here are using pycopy. You'll probably get a better response looking in that project's resources.

I'm also unclear whether your firmware version is pycopy. The pycopy libraries can only be expected to work with pycopy firmware. You can't generally mix'n'match, and the pycopy uasyncio is an entirely different implementation to that built in to official firmware.
Peter Hinch
Index to my micropython libraries.

leosok
Posts: 18
Joined: Sun May 02, 2021 10:10 pm

Re: uasyncio.open_connection and ssl

Post by leosok » Mon May 03, 2021 10:32 am

Hi Peter, thanks for the quick reply! I did not realize I could not mix'n'match and that asyncio was different in pycopy & official Micropython. It seems to me that wrapping the socket with ussl should work in the official asyncio too. I wonder how I could try to patch it, without building the firmware myself.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio.open_connection and ssl

Post by pythoncoder » Thu May 06, 2021 7:31 am

Official uasyncio.open_connection returns a stream based on a nonblocking socket. SSL/TLS support for nonblocking sockets is something of a work-in-progress. In my experience it works on the Pyboard D, but (as far as I know) it doesn't yet on ESP32 or ESP8266. It has priority - see this comment - but hasn't yet been implemented.
Peter Hinch
Index to my micropython libraries.

Post Reply