forked from juho-p/wot-debugserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcprepl.py
67 lines (56 loc) · 1.58 KB
/
tcprepl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
write_client = None
import socket
PORT = 2222
def run_repl():
'''
Run debug server until connection is closed
'''
global write_client
newline = '\r\n'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('127.0.0.1', PORT))
s.listen(1)
def repl(filestream, once=False):
global write_client
import wotdbg
def echo(s):
filestream.write(str(s))
filestream.write(newline)
filestream.flush()
write_client = echo
local_vars = {'echo': echo, 'wotdbg': wotdbg}
for line in filestream:
line = line.strip()
if line == 'QUIT':
break
try:
try:
try_exec = False
res = eval(line, local_vars)
echo(res)
except SyntaxError:
try_exec = True
if try_exec:
exec line in local_vars
except Exception, e:
import traceback
echo(traceback.format_exc())
readymsg = local_vars.get('__READYMSG', None)
if readymsg is not None:
echo(readymsg)
if once:
break
write_client = None
conn = f = None
try:
conn, addr = s.accept()
f = conn.makefile()
repl(f)
finally:
s.close()
if conn != None:
conn.close()
f.close()
if __name__ == '__main__':
run_repl()