10000 Simple example of uasyncio WebSocket Server (secure - with SSL) · Issue #8177 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Simple example of uasyncio WebSocket Server (secure - with SSL) #8177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
beyonlo opened this issue Jan 14, 2022 · 10 comments
Open

Simple example of uasyncio WebSocket Server (secure - with SSL) #8177

beyonlo opened this issue Jan 14, 2022 · 10 comments

Comments

@beyonlo
Copy link
beyonlo commented Jan 14, 2022

Hi all.

I would like a simple example of uasyncio WebSocket Server (secure - with SSL). I see that there is a official uwebsocket module implemented on MicroPython.

>>> import os
>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.17.0', version='v1.17 on 2021-09-02', machine='ESP32 module (spiram) with ESP32')
>>> import uwebsocket
>>>

Well, so I found a example (https://github.com/micropython/micropython/blob/master/extmod/webrepl/webrepl.py) that use the official uwebsocket module. Need just to add SSL + uasyncio on that example.

Thank you in advance!

@Tangerino
Copy link

You can look here. Not sure about quality, never used.

https://github.com/jczic/MicroWebSrv

@beyonlo
Copy link
Author
beyonlo commented Jan 16, 2022

You can look here. Not sure about quality, never used.

https://github.com/jczic/MicroWebSrv

@Tangerino

That use thread, not uasyncio.

@bazooka07
Copy link

Look at Picoweb. It uses uasyncio by Paul Sokolovsky.
https://github.com/pfalcon/picoweb
For ssl, I don't know. But it has an example with basic authentication.

@beyonlo beyonlo closed this as completed Jan 19, 2022
@beyonlo beyonlo reopened this Jan 19, 2022
@beyonlo
Copy link
Author
beyonlo commented Jan 19, 2022

Hello @bazooka07

Look at Picoweb. It uses uasyncio by Paul Sokolovsky. https://github.com/pfalcon/picoweb For ssl, I don't know. But it has an example with basic authentication.

The Picoweb do not support websocket. Even uasyncio on PicoWeb is old, it is not the official v3 uasyncio compatible, but it is good start point.

The https://github.com/jczic/MicroWebSrv use WebSocket, but is not official uwebsocket module - it wrote their own websocket module. And is better (if is possible) to use the official, for compatibility and so on.

This project https://github.com/Carglglz/upydev/tree/master/upyutils has a WebREPL example working with SSL using the official uwebsocket module - that is great. This project was put as a PR (#5611) to support SSL in the official WebREPL, but I do not know why still not was merged, after two years.

I need just to know now how to add uasyncio on that WebREPL + SSL example.

@QGB
8000 Copy link
QGB commented Oct 6, 2022
'Stream' object has no attribute 'ios'

@Carglglz
Copy link
Contributor

@beyonlo FYI with the latest changes to #8968 I'm able to run a microdot_async + tls (https) server and WebREPL + tls (wss) + aiorepl at the same time, so I think the uasyncio WebSocket Server (secure - with SSL) should be possible too. 👍🏼

@beyonlo
Copy link
Author
beyonlo commented Dec 23, 2022

@Carglglz Excellent!

so I think the uasyncio WebSocket Server (secure - with SSL) should be possible too. 👍🏼

Sorry for my ignorance, but think that already working on the Microdot, like as show this example echo_async_tls.py working with this index.html that use wss (WebSocket Secure Server).

Please, let me know what I missing. I'm a bit confuse now: how that (uasyncio WebSocket Server (secure - with SSL)) already working on Microdot if the #8968 is a new implementation to have that working?

Thank you in advance!

@Carglglz
Copy link
Contributor

@beyonlo I think those examples only work with CPython at the moment?, unless I'm missing something in that example

import ssl
from microdot_asyncio import Microdot, send_file
from microdot_asyncio_websocket import with_websocket

app = Microdot()


@app.route('/')
def index(request):
    return send_file('index.html')


@app.route('/echo')
@with_websocket
async def echo(request, ws):
    while True:
        data = await ws.receive()
        await ws.send(data)


sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
sslctx.load
8000
_cert_chain('cert.pem', 'key.pem')
app.run(port=4443, debug=True, ssl=sslctx)

Right now MicroPython ssl module has no SSLContext, and in app.run

This calls

asyncio.run(self.start_server(host=host, port=port, debug=debug,
                                      ssl=ssl))

which calls

try:
         self.server = await asyncio.start_server(serve, host, port,
                                                     ssl=ssl)
 except TypeError:
            self.server = await asyncio.start_server(serve, host, port)

And if you look at asyncio.start_server in MicroPython extmod/uasyncio/stream.py there is no ssl option

async def start_server(cb, host, port, backlog=5):

Also this comment from @miguelgrinberg in the issue you opened in #34

Would appreciate it if you test it and provide feedback. Note that the async support cannot currently be used with MicroPython, since uasyncio does not have the necessary support

Well, with the latest changes to #8968 the async support is enabled, so those examples will work in MicroPython too. 😄

@beyonlo
Copy link
Author
beyonlo commented Dec 24, 2022

@Carglglz Ohh, you are right, that echo_async_tls.py do not works with MicroPython. As I still developing using as base example just the echo_async.py (WebSocket Server without SSL) and just when the entire application will be stable that I was thinking do add the ssl, that was the reason that I never had tested the echo_async_tls.py on the MicroPython.

Also this comment from @miguelgrinberg in the issue you opened in #34

Would appreciate it if you test it and provide feedback. Note that the async support cannot currently be used with MicroPython, since uasyncio does not have the necessary support

Yes, I remember now about that moment. Maybe a good idea, as suggstion, is @miguelgrinberg add a note in the that README.md (inside tls examples) talking about that the echo_async_tls.py works (for now :) ) just on the CPython.

Well, with the latest changes to #8968 the async support is enabled, so those examples will work in MicroPython too. smile

Excellent, so maybe sooner the echo_async_tls.py will works on MicroPython as well :)

Thank you for the explanation!

tannewt pushed a commit to tannewt/circuitpython that referenced this issue Jul 18, 2023
…n-main

Translations update from Hosted Weblate
@dpgeorge
Copy link
Member

Asyncio now supports SSL server and client mode. So that's the first and main step to getting secure WebSockets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0