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()
40
def execCmd(self, cmd):
42
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
46
res = eval(cmd, self.globs, self.locls)
48
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
50
self.out = cStringIO.StringIO()
51
except Exception, exc:
53
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
55
self.out = cStringIO.StringIO()
59
compiler = codeop.CommandCompiler()
64
if self.curr_cmd == '':
65
self.curr_cmd = ln['chat']
67
self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
69
cmd = compiler(self.curr_cmd)
71
# The command was incomplete,
72
# so send back a None, so the
73
# client can print a '...'
74
self.cmdQ.put({"more":None})
77
except Exception, exc:
79
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
81
self.out = cStringIO.StringIO()
83
# throw away a partial command.
85
cmd = compile(ln['block'], "<web session>", 'exec');
87
except Exception, exc:
89
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
91
self.out = cStringIO.StringIO()
95
self.globs['__builtins__'] = globals()['__builtins__']
100
if os.fork(): # launch child and...
101
os._exit(0) # kill off parent
103
if os.fork(): # launch child and...
104
os._exit(0) # kill off parent again.
107
# The global 'magic' is the secret that the client and server share
108
# which is used to create and md5 digest to authenticate requests.
109
# It is assigned a real value at startup.
113
lineQ = Queue.Queue()
114
interpThread = PythonRunner(cmdQ, lineQ)
116
if __name__ == "__main__":
117
port = int(sys.argv[1])
120
# Attempt to open the socket.
121
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
125
# Excellent! It worked. Let's turn ourself into a daemon,
126
# then get on with the job of being a python interpreter.
129
interpThread.setDaemon(True)
133
(conn, addr) = s.accept()
136
buf = cStringIO.StringIO()
137
blk = conn.recv(1024)
141
blk = conn.recv(1024, socket.MSG_DONTWAIT)
143
# Exception thrown if it WOULD block (but we
144
# told it not to wait) - ie. we are done
147
msg = cjson.decode(inp)
149
# Check that the message is
150
digest = md5.new(msg['text'] + magic).digest().encode('hex')
151
if msg['digest'] != digest:
155
lineQ.put({msg['cmd']:msg['text']})
157
conn.sendall(cjson.encode(r))