16
globs['__builtins__'] = globals()['__builtins__']
18
compiler = codeop.CommandCompiler()
26
curr_cmd = curr_cmd + '\n' + txt
28
cmd = compiler(curr_cmd)
30
# The command was incomplete,
31
# so send back a None, so the
32
# client can print a '...'
33
web.output(cjson.encode(None))
35
# The command was complete,
37
out = cStringIO.StringIO()
41
res = eval(cmd, globs, locls)
43
v = (out.getvalue(), res, None)
44
web.output(cjson.encode(v))
46
except Exception, exc:
15
from threading import Thread
17
class StdinFromWeb(object):
18
def __init__(self, cmdQ, lineQ):
48
v = (None, None, str(exc))
49
web.output(cjson.encode(v))
25
self.cmdQ.put({"input":None})
28
# Some of our 5 seconds may have elapsed, but
33
class PythonRunner(Thread):
34
def __init__(self, cmdQ, lineQ):
41
compiler = codeop.CommandCompiler()
45
if self.curr_cmd == '':
48
self.curr_cmd = self.curr_cmd + '\n' + l
50
cmd = compiler(self.curr_cmd)
52
# The command was incomplete,
53
# so send back a None, so the
54
# client can print a '...'
55
self.cmdQ.put({"more":None})
57
# The command was complete,
59
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
60
out = cStringIO.StringIO()
64
res = eval(cmd, globs, locls)
66
self.cmdQ.put({"okay":(out.getvalue(),res)})
68
except Exception, exc:
70
self.cmdQ.put({"exc":str(exc)})
75
self.globs['__builtins__'] = globals()['__builtins__']
98
125
# Okay, so the authentication succeeded,
99
126
# so now we have the trivial matter of actually
100
127
# executing the python....
130
sys.__stderr__.write(cjson.encode(r) + "\n")
131
web.output(cjson.encode(r))
134
lineQ = Queue.Queue()
135
interpThread = PythonRunner(cmdQ, lineQ)
103
137
if __name__ == "__main__":
105
138
magic = sys.argv[2]
139
interpThread.setDaemon(True)
106
141
web.run(urls, globals())