2016-04-24 16:04:21 -04:00
|
|
|
# This module should be imported from REPL, not run from command line.
|
|
|
|
import socket
|
|
|
|
import uos
|
2016-04-25 17:59:21 -04:00
|
|
|
import network
|
2016-04-24 16:04:21 -04:00
|
|
|
import websocket
|
|
|
|
import websocket_helper
|
2016-04-30 13:39:35 -04:00
|
|
|
import _webrepl
|
2016-04-24 16:04:21 -04:00
|
|
|
|
|
|
|
listen_s = None
|
|
|
|
client_s = None
|
|
|
|
|
2016-04-30 13:41:09 -04:00
|
|
|
def setup_conn(port, accept_handler):
|
2016-04-24 16:04:21 -04:00
|
|
|
global listen_s, client_s
|
|
|
|
listen_s = socket.socket()
|
|
|
|
listen_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
2016-04-25 11:44:37 -04:00
|
|
|
ai = socket.getaddrinfo("0.0.0.0", port)
|
2016-04-24 16:04:21 -04:00
|
|
|
addr = ai[0][4]
|
|
|
|
|
|
|
|
listen_s.bind(addr)
|
|
|
|
listen_s.listen(1)
|
2016-04-30 13:41:09 -04:00
|
|
|
listen_s.setsockopt(socket.SOL_SOCKET, 20, accept_handler)
|
2016-04-25 17:59:21 -04:00
|
|
|
for i in (network.AP_IF, network.STA_IF):
|
|
|
|
iface = network.WLAN(i)
|
|
|
|
if iface.active():
|
2016-04-26 05:47:24 -04:00
|
|
|
print("WebREPL daemon started on ws://%s:%d" % (iface.ifconfig()[0], port))
|
2016-04-24 16:04:21 -04:00
|
|
|
|
|
|
|
|
2016-04-24 17:31:43 -04:00
|
|
|
def accept_conn(listen_sock):
|
|
|
|
global client_s
|
|
|
|
cl, remote_addr = listen_sock.accept()
|
2016-04-25 18:00:28 -04:00
|
|
|
print("\nWebREPL connection from:", remote_addr)
|
2016-04-24 17:31:43 -04:00
|
|
|
client_s = cl
|
2016-04-24 16:04:21 -04:00
|
|
|
websocket_helper.server_handshake(cl)
|
|
|
|
ws = websocket.websocket(cl, True)
|
2016-04-30 13:39:35 -04:00
|
|
|
ws = _webrepl._webrepl(ws)
|
2016-04-24 16:04:21 -04:00
|
|
|
cl.setblocking(False)
|
|
|
|
# notify REPL on socket incoming data
|
|
|
|
cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify)
|
|
|
|
uos.dupterm(ws)
|
2016-04-24 17:31:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
global listen_s, client_s
|
|
|
|
uos.dupterm(None)
|
|
|
|
if client_s:
|
|
|
|
client_s.close()
|
|
|
|
if listen_s:
|
|
|
|
listen_s.close()
|
|
|
|
|
|
|
|
|
2016-05-07 14:59:02 -04:00
|
|
|
def start(port=8266, password=None):
|
2016-04-24 17:31:43 -04:00
|
|
|
stop()
|
2016-05-07 14:59:02 -04:00
|
|
|
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)
|
2016-04-30 13:41:09 -04:00
|
|
|
setup_conn(port, accept_conn)
|
|
|
|
print("Started webrepl in normal mode")
|