Page 1 of 1

Anyone using python websockets to connect to WebREPL?

Posted: Thu Aug 25, 2022 2:27 am
by spierepf
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.

Re: Anyone using python websockets to connect to WebREPL?

Posted: Thu Aug 25, 2022 2:55 am
by bulletmark
I use websockets-client which works for me with ESP32's.

Re: Anyone using python websockets to connect to WebREPL?

Posted: Thu Aug 25, 2022 3:22 am
by spierepf
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.

Re: Anyone using python websockets to connect to WebREPL?

Posted: Thu Aug 25, 2022 3:38 am
by bulletmark
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.

Re: Anyone using python websockets to connect to WebREPL?

Posted: Fri Aug 26, 2022 3:14 am
by spierepf
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?

Re: Anyone using python websockets to connect to WebREPL?

Posted: Fri Aug 26, 2022 3:29 am
by bulletmark
That "/r" should be "\r\n".

Re: Anyone using python websockets to connect to WebREPL?

Posted: Fri Feb 03, 2023 10:55 am
by ihornehrutsa

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()