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})
29
# Some of our 5 seconds may have elapsed, but never mind.
33
class PythonRunner(Thread):
34
def __init__(self, cmdQ, lineQ):
37
self.out = cStringIO.StringIO()
42
compiler = codeop.CommandCompiler()
47
if self.curr_cmd == '':
48
self.curr_cmd = ln['chat']
50
self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
52
cmd = compiler(self.curr_cmd)
54
# The command was incomplete,
55
# so send back a None, so the
56
# client can print a '...'
57
self.cmdQ.put({"more":None})
59
# The command was complete,
61
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
65
res = eval(cmd, self.globs, self.locls)
67
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
69
self.out = cStringIO.StringIO()
70
except Exception, exc:
72
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
74
self.out = cStringIO.StringIO()
76
# throw away a partial command.
78
cmd = compile(ln['block'], "<web session>", 'exec');
80
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
81
self.out = cStringIO.StringIO()
85
res = eval(cmd, self.globs, self.locls)
87
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
89
self.out = cStringIO.StringIO()
90
except Exception, exc:
92
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
94
self.out = cStringIO.StringIO()
98
self.globs['__builtins__'] = globals()['__builtins__']
103
if os.fork(): # launch child and...
104
os._exit(0) # kill off parent
106
if os.fork(): # launch child and...
107
os._exit(0) # kill off parent again.
110
# The global 'magic' is the secret that the client and server share
111
# which is used to create and md5 digest to authenticate requests.
112
# It is assigned a real value at startup.
116
lineQ = Queue.Queue()
117
interpThread = PythonRunner(cmdQ, lineQ)
119
if __name__ == "__main__":
120
port = int(sys.argv[1])
123
# Attempt to open the socket.
124
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
128
# Excellent! It worked. Let's turn ourself into a daemon,
129
# then get on with the job of being a python interpreter.
132
interpThread.setDaemon(True)
136
(conn, addr) = s.accept()
139
buf = cStringIO.StringIO()
140
blk = conn.recv(1024)
144
blk = conn.recv(1024, socket.MSG_DONTWAIT)
146
# Exception thrown if it WOULD block (but we
147
# told it not to wait) - ie. we are done
150
msg = cjson.decode(inp)
152
# Check that the message is
153
digest = md5.new(msg['text'] + magic).digest().encode('hex')
154
if msg['digest'] != digest:
158
lineQ.put({msg['cmd']:msg['text']})
160
conn.sendall(cjson.encode(r))