~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to scripts/python-console

  • Committer: drtomc
  • Date: 2008-02-06 03:21:36 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:432
usrmgt: more work on this. Still some work to go.
console: refactored to use the ivle-chat protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import sys
15
15
from threading import Thread
16
16
 
 
17
import common.chat
 
18
 
17
19
class StdinFromWeb(object):
18
20
    def __init__(self, cmdQ, lineQ):
19
21
        self.cmdQ = cmdQ
113
115
lineQ = Queue.Queue()
114
116
interpThread = PythonRunner(cmdQ, lineQ)
115
117
 
 
118
def initializer():
 
119
    interpThread.setDaemon(True)
 
120
    interpThread.start()
 
121
 
 
122
def dispatch_msg(msg):
 
123
    lineQ.put({msg['cmd']:msg['text']})
 
124
    return cmdQ.get()
 
125
 
116
126
if __name__ == "__main__":
117
127
    port = int(sys.argv[1])
118
128
    magic = sys.argv[2]
119
129
 
120
 
    # Attempt to open the socket.
121
 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
122
 
    s.bind(('', port))
123
 
    s.listen(1)
124
 
 
125
 
    # Excellent! It worked. Let's turn ourself into a daemon,
126
 
    # then get on with the job of being a python interpreter.
127
 
    daemonize()
128
 
 
129
 
    interpThread.setDaemon(True)
130
 
    interpThread.start()
131
 
 
132
 
    while True:
133
 
        (conn, addr) = s.accept()
134
 
        try:
135
 
            # Grab the input
136
 
            buf = cStringIO.StringIO()
137
 
            blk = conn.recv(1024)
138
 
            while blk:
139
 
                buf.write(blk)
140
 
                try:
141
 
                    blk = conn.recv(1024, socket.MSG_DONTWAIT)
142
 
                except:
143
 
                    # Exception thrown if it WOULD block (but we
144
 
                    # told it not to wait) - ie. we are done
145
 
                    blk = None
146
 
            inp = buf.getvalue()
147
 
            msg = cjson.decode(inp)
148
 
            
149
 
            # Check that the message is 
150
 
            digest = md5.new(msg['text'] + magic).digest().encode('hex')
151
 
            if msg['digest'] != digest:
152
 
                conn.close()
153
 
                continue
154
 
 
155
 
            lineQ.put({msg['cmd']:msg['text']})
156
 
            r = cmdQ.get()
157
 
            conn.sendall(cjson.encode(r))
158
 
            conn.close()
159
 
        except Exception, e:
160
 
            conn.close()
 
130
    common.chat.start_server(port, magic, True, dispatch_msg, initializer)