diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-07-02 22:45:31 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-07-02 22:45:31 +0300 |
commit | b8f45166c631d67f9426fc1a561ab9f27c16cb1d (patch) | |
tree | 19d10686d51c7cb27408340cb65fbe1c244feeb1 /esp8266/modules | |
parent | dec51e3519ac6f8ab2ffd0cf00407a505742248f (diff) | |
download | micropython-b8f45166c631d67f9426fc1a561ab9f27c16cb1d.tar.gz micropython-b8f45166c631d67f9426fc1a561ab9f27c16cb1d.zip |
esp8266: Switch webrepl to use frozen bytecode.
Diffstat (limited to 'esp8266/modules')
-rw-r--r-- | esp8266/modules/webrepl.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/esp8266/modules/webrepl.py b/esp8266/modules/webrepl.py new file mode 100644 index 0000000000..da3e70c595 --- /dev/null +++ b/esp8266/modules/webrepl.py @@ -0,0 +1,75 @@ +# This module should be imported from REPL, not run from command line. +import socket +import uos +import network +import websocket +import websocket_helper +import _webrepl + +listen_s = None +client_s = None + +def setup_conn(port, accept_handler): + global listen_s + listen_s = socket.socket() + listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + ai = socket.getaddrinfo("0.0.0.0", port) + addr = ai[0][4] + + listen_s.bind(addr) + listen_s.listen(1) + if accept_handler: + listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler) + for i in (network.AP_IF, network.STA_IF): + iface = network.WLAN(i) + if iface.active(): + print("WebREPL daemon started on ws://%s:%d" % (iface.ifconfig()[0], port)) + return listen_s + + +def accept_conn(listen_sock): + global client_s + cl, remote_addr = listen_sock.accept() + print("\nWebREPL connection from:", remote_addr) + client_s = cl + websocket_helper.server_handshake(cl) + ws = websocket.websocket(cl, True) + ws = _webrepl._webrepl(ws) + cl.setblocking(False) + # notify REPL on socket incoming data + cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify) + uos.dupterm(ws) + + +def stop(): + global listen_s, client_s + uos.dupterm(None) + if client_s: + client_s.close() + if listen_s: + listen_s.close() + + +def start(port=8266, password=None): + stop() + if password is None: + try: + import port_config + _webrepl.password(port_config.WEBREPL_PASS) + setup_conn(port, accept_conn) + print("Started webrepl in normal mode") + except: + import webrepl_setup + setup_conn(port, webrepl_setup.handle_conn) + print("Started webrepl in setup mode") + else: + _webrepl.password(password) + setup_conn(port, accept_conn) + print("Started webrepl in normal mode") + + +def start_foreground(port=8266): + stop() + s = setup_conn(port, None) + accept_conn(s) |