4
4
# python-console <port> <magic>
15
from threading import Thread
17
class StdinFromWeb(object):
18
def __init__(self, cmdQ, lineQ):
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__']
12
globs['__builtins__'] = globals()['__builtins__']
14
compiler = codeop.CommandCompiler()
21
curr_cmd = curr_cmd + '\n' + txt
23
cmd = compiler(curr_cmd)
25
# The command was incomplete,
26
# so send back a None, so the
27
# client can print a '...'
28
web.output(cjson.encode(None))
30
# The command was complete,
32
out = cStringIO.StringIO()
36
res = eval(cmd, globs, locls)
38
v = (out.getvalue(), res, None)
39
web.output(json.encode(v))
41
except Exception, exc:
42
v = (None, None, str(exc))
43
web.output(json.encode(v))
81
'/index.html', 'index',
82
48
'/(.*\.js)', 'jscript',
83
49
'/(.*\.css)', 'style',
112
78
web.output(file(name, "r").read())
120
85
digest = md5.new(inp.text + magic).digest().encode('hex')
121
86
if inp.digest != digest:
122
web.output("401 Unauthorized")
123
87
web.ctx.status = '401 Unauthorized'
126
90
# Okay, so the authentication succeeded,
127
91
# so now we have the trivial matter of actually
128
92
# executing the python....
131
sys.__stderr__.write(cjson.encode(r) + "\n")
132
web.output(cjson.encode(r))
135
lineQ = Queue.Queue()
136
interpThread = PythonRunner(cmdQ, lineQ)
93
web.output(do_chat(inp.text))
138
95
if __name__ == "__main__":
139
97
magic = sys.argv[2]
140
interpThread.setDaemon(True)
142
98
web.run(urls, globals())