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__']
81
'/index.html', 'index',
82
'/(.*\.js)', 'jscript',
83
'/(.*\.css)', 'style',
86
# The global 'magic' is the secret that the client and server share
87
# which is used to create and md5 digest to authenticate requests.
88
# It is assigned a real value at startup.
96
digest = md5.new('hello' + magic).digest().encode('hex')
97
if inp.digest != digest:
98
web.ctx.status = '401 Unauthorized'
101
# Okay, so the authentication succeeded,
102
# so all we need to do is send back the static
103
# HTML for the console app.
104
web.output(file("index.html", "r").read())
108
web.output(file(name, "r").read())
112
web.output(file(name, "r").read())
120
digest = md5.new(inp.text + magic).digest().encode('hex')
121
if inp.digest != digest:
122
web.output("401 Unauthorized")
123
web.ctx.status = '401 Unauthorized'
126
# Okay, so the authentication succeeded,
127
# so now we have the trivial matter of actually
128
# 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)
138
if __name__ == "__main__":
140
interpThread.setDaemon(True)
142
web.run(urls, globals())