4
# python-console <port> <magic>
15
from threading import Thread
19
class StdinFromWeb(object):
20
def __init__(self, cmdQ, lineQ):
27
self.cmdQ.put({"input":None})
31
# Some of our 5 seconds may have elapsed, but never mind.
35
class PythonRunner(Thread):
36
def __init__(self, cmdQ, lineQ):
39
self.out = cStringIO.StringIO()
42
def execCmd(self, cmd):
44
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
48
res = eval(cmd, self.globs, self.locls)
50
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
52
self.out = cStringIO.StringIO()
53
except Exception, exc:
55
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
57
self.out = cStringIO.StringIO()
61
compiler = codeop.CommandCompiler()
66
if self.curr_cmd == '':
67
self.curr_cmd = ln['chat']
69
self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
71
cmd = compiler(self.curr_cmd)
73
# The command was incomplete,
74
# so send back a None, so the
75
# client can print a '...'
76
self.cmdQ.put({"more":None})
79
except Exception, exc:
81
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
83
self.out = cStringIO.StringIO()
85
# throw away a partial command.
87
cmd = compile(ln['block'], "<web session>", 'exec');
89
except Exception, exc:
91
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
93
self.out = cStringIO.StringIO()
97
self.globs['__builtins__'] = globals()['__builtins__']
102
if os.fork(): # launch child and...
103
os._exit(0) # kill off parent
105
if os.fork(): # launch child and...
106
os._exit(0) # kill off parent again.
109
# The global 'magic' is the secret that the client and server share
110
# which is used to create and md5 digest to authenticate requests.
111
# It is assigned a real value at startup.
115
lineQ = Queue.Queue()
116
interpThread = PythonRunner(cmdQ, lineQ)
119
interpThread.setDaemon(True)
122
def dispatch_msg(msg):
123
lineQ.put({msg['cmd']:msg['text']})
126
if __name__ == "__main__":
127
port = int(sys.argv[1])
130
common.chat.start_server(port, magic, True, dispatch_msg, initializer)