Anyone using python websockets to connect to WebREPL?

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
spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Anyone using python websockets to connect to WebREPL?

Post by spierepf » Thu Aug 25, 2022 2:27 am

I'm trying to use the python websockets library (https://websockets.readthedocs.io/en/stable/) to connect to the WebREPL on my ESP32.

However, I keep getting

Code: Select all

websockets.exceptions.ConnectionClosedError: sent 1011 (unexpected error) keepalive ping timeout; no close frame received
exceptions when I try to read from the device.

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: Anyone using python websockets to connect to WebREPL?

Post by bulletmark » Thu Aug 25, 2022 2:55 am

I use websockets-client which works for me with ESP32's.

spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Re: Anyone using python websockets to connect to WebREPL?

Post by spierepf » Thu Aug 25, 2022 3:22 am

Are you using the raw REPL protocol? And if so, can you post an example? For me, websocket_client just hangs in the same place that websockets throws that exception.

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: Anyone using python websockets to connect to WebREPL?

Post by bulletmark » Thu Aug 25, 2022 3:38 am

Not sure what you are asking but I have implemented a REPL mode although I don't support raw (Ctrl+A) or paste (Ctrl+E) modes. Can't give a simple example but nothing tricky stands out. I just set a message handler and process each received string.

spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Re: Anyone using python websockets to connect to WebREPL?

Post by spierepf » Fri Aug 26, 2022 3:14 am

This is the code I'm trying to use to connect:

Code: Select all

import pytest
import websocket

from .credentials import WEBREPL_CREDENTIALS


@pytest.fixture
def client():
    ws = websocket.WebSocket()
    ws.connect(WEBREPL_CREDENTIALS["url"])
    assert "Password: " == ws.recv()
    ws.send_binary((WEBREPL_CREDENTIALS["password"]+"/r").encode("utf-8"))
    print(ws.recv())


def test_eval(client):
    pass
However, the call to print(ws.recv()) just hangs, my device never responds to the password.

Any suggestions?

bulletmark
Posts: 59
Joined: Mon Mar 29, 2021 1:36 am
Location: Brisbane Australia

Re: Anyone using python websockets to connect to WebREPL?

Post by bulletmark » Fri Aug 26, 2022 3:29 am

That "/r" should be "\r\n".

ihornehrutsa
Posts: 35
Joined: Sat Oct 26, 2019 8:38 pm

Re: Anyone using python websockets to connect to WebREPL?

Post by ihornehrutsa » Fri Feb 03, 2023 10:55 am

Code: Select all

# websocket_REPL_test.py
# Install websocket-client:
# pip install websocket-client

CRLF = b"\r\n"

command = b'''
1+1
2+2

'''

PASS = b'' + CRLF

IP = '172.16.11.51'

import websocket
import _thread
import time

in_message = ''

def on_message(ws, message):
    print(message, end='')

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        #ws.settimeout(20)
        ws.send(PASS)
        
        for l in command.splitlines():
            cmd = l + CRLF
        
            ws.send(cmd)
            
            time.sleep(2)
            
        ws.close()
        print("thread terminating...")
        
    _thread.start_new_thread(run, ())

if __name__ == "__main__":
    #websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://" + IP + ":8266",
                              on_open=on_open,
                              on_message=on_message,
                              on_error=on_error,
                              on_close=on_close)

    ws.run_forever()


Post Reply