diff options
author | Damien George <damien.p.george@gmail.com> | 2017-09-06 13:40:51 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-09-06 13:40:51 +1000 |
commit | 01dd7804b87d60b2deab16712eccb3b97351a9b7 (patch) | |
tree | 1aa21f38a872b8e62a3d4e4f74f68033c6f827e4 /ports/esp8266/modules/webrepl.py | |
parent | a9862b30068fc9df1022f08019fb35aaa5085f64 (diff) |
ports: Make new ports/ sub-directory and move all ports there.
This is to keep the top-level directory clean, to make it clear what is
core and what is a port, and to allow the repository to grow with new ports
in a sustainable way.
Diffstat (limited to 'ports/esp8266/modules/webrepl.py')
-rw-r--r-- | ports/esp8266/modules/webrepl.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/ports/esp8266/modules/webrepl.py b/ports/esp8266/modules/webrepl.py new file mode 100644 index 000000000..5a76e9b26 --- /dev/null +++ b/ports/esp8266/modules/webrepl.py @@ -0,0 +1,77 @@ +# 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() + if uos.dupterm(): + print("\nConcurrent WebREPL connection from", remote_addr, "rejected") + cl.close() + return + 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 webrepl_cfg + _webrepl.password(webrepl_cfg.PASS) + setup_conn(port, accept_conn) + print("Started webrepl in normal mode") + except: + print("WebREPL is not configured, run 'import webrepl_setup'") + else: + _webrepl.password(password) + setup_conn(port, accept_conn) + print("Started webrepl in manual override mode") + + +def start_foreground(port=8266): + stop() + s = setup_conn(port, None) + accept_conn(s) |