68
67
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
69
self.out = cStringIO.StringIO()
70
70
except Exception, exc:
72
72
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
74
self.out = cStringIO.StringIO()
75
76
# throw away a partial command.
78
78
cmd = compile(ln['block'], "<web session>", 'exec');
80
80
sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
81
81
self.out = cStringIO.StringIO()
85
85
res = eval(cmd, self.globs, self.locls)
87
87
self.cmdQ.put({"okay":(self.out.getvalue(),res)})
89
self.out = cStringIO.StringIO()
89
90
except Exception, exc:
91
92
self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
94
self.out = cStringIO.StringIO()
93
96
def init_state(self):
97
100
self.curr_cmd = ''
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.
103
110
# The global 'magic' is the secret that the client and server share
104
111
# which is used to create and md5 digest to authenticate requests.
105
112
# It is assigned a real value at startup.
114
digest = md5.new(inp.text + magic).digest().encode('hex')
115
if inp.digest != digest:
116
web.output("401 Unauthorized")
117
web.ctx.status = '401 Unauthorized'
120
# Okay, so the authentication succeeded,
121
# so now we have the trivial matter of actually
122
# executing the python....
123
lineQ.put({'chat':inp.text})
125
sys.__stderr__.write(cjson.encode(r) + "\n")
126
web.output(cjson.encode(r))
134
digest = md5.new(inp.text + magic).digest().encode('hex')
135
if inp.digest != digest:
136
web.output("401 Unauthorized")
137
web.ctx.status = '401 Unauthorized'
140
# Okay, so the authentication succeeded,
141
# so now we have the trivial matter of actually
142
# executing the python....
143
lineQ.put({'block':inp.text})
145
sys.__stderr__.write(cjson.encode(r) + "\n")
146
web.output(cjson.encode(r))
148
115
cmdQ = Queue.Queue()
149
116
lineQ = Queue.Queue()
150
117
interpThread = PythonRunner(cmdQ, lineQ)
152
119
if __name__ == "__main__":
120
port = int(sys.argv[1])
153
121
magic = sys.argv[2]
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.
154
132
interpThread.setDaemon(True)
155
133
interpThread.start()
156
web.run(urls, globals())
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))