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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: mattgiuca
  • Date: 2008-01-12 15:35:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:201
Added "test" directory.
Added make_date_test.py, a short script I wrote to test the date format
algorithm committed in the previous revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import cgi
12
12
import cStringIO
13
13
import signal
14
 
import Queue
15
 
from threading import Thread
16
 
 
17
 
class StdinFromWeb(object):
18
 
    def __init__(self, cmdQ, lineQ):
19
 
        self.cmdQ = cmdQ
20
 
        self.lineQ = lineQ
21
 
 
22
 
    def readline(self):
23
 
        # stop the clock!
 
14
 
 
15
globs = {}
 
16
globs['__builtins__'] = globals()['__builtins__']
 
17
locls = {}
 
18
compiler = codeop.CommandCompiler()
 
19
curr_cmd = ''
 
20
 
 
21
def do_chat(txt):
 
22
    global curr_cmd
 
23
    if curr_cmd == '':
 
24
        curr_cmd = txt
 
25
    else:
 
26
        curr_cmd = curr_cmd + '\n' + txt
 
27
    try:
 
28
        cmd = compiler(curr_cmd)
 
29
        if cmd is None:
 
30
            # The command was incomplete,
 
31
            # so send back a None, so the
 
32
            # client can print a '...'
 
33
            web.output(cjson.encode(None))
 
34
        else:
 
35
            # The command was complete,
 
36
            # so evaluate it!
 
37
            out = cStringIO.StringIO()
 
38
            sys.stdout = out
 
39
            sys.stderr = out
 
40
            signal.alarm(5)
 
41
            res = eval(cmd, globs, locls)
 
42
            signal.alarm(0)
 
43
            v = (out.getvalue(), res, None)
 
44
            web.output(cjson.encode(v))
 
45
            curr_cmd = ''
 
46
    except Exception, exc:
24
47
        signal.alarm(0)
25
 
        self.cmdQ.put({"input":None})
26
 
        ln = self.lineQ.get()
27
 
        if 'chat' in ln:
28
 
            # restart the clock:
29
 
            # Some of our 5 seconds may have elapsed, but never mind.
30
 
            signal.alarm(5)
31
 
            return ln['chat']
32
 
 
33
 
class PythonRunner(Thread):
34
 
    def __init__(self, cmdQ, lineQ):
35
 
        self.cmdQ = cmdQ
36
 
        self.lineQ = lineQ
37
 
        Thread.__init__(self)
38
 
 
39
 
    def run(self):
40
 
        self.init_state()
41
 
        compiler = codeop.CommandCompiler()
42
 
 
43
 
        while True:
44
 
            ln = self.lineQ.get()
45
 
            if 'chat' in ln:
46
 
                mode = 
47
 
                if self.curr_cmd == '':
48
 
                    self.curr_cmd = ln['chat']
49
 
                else:
50
 
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
51
 
                try:
52
 
                    cmd = compiler(self.curr_cmd)
53
 
                    if cmd is None:
54
 
                        # The command was incomplete,
55
 
                        # so send back a None, so the
56
 
                        # client can print a '...'
57
 
                        self.cmdQ.put({"more":None})
58
 
                    else:
59
 
                        # The command was complete,
60
 
                        # so evaluate it!
61
 
                        sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
62
 
                        self.out = cStringIO.StringIO()
63
 
                        sys.stdout = out
64
 
                        sys.stderr = out
65
 
                        signal.alarm(5)
66
 
                        res = eval(cmd, globs, locls)
67
 
                        signal.alarm(0)
68
 
                        self.cmdQ.put({"okay":(self.out.getvalue(),res)})
69
 
                        self.curr_cmd = ''
70
 
                except Exception, exc:
71
 
                    signal.alarm(0)
72
 
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
73
 
                    self.curr_cmd = ''
74
 
            if 'block' in ln:
75
 
                # throw away a partial command.
76
 
                self.curr_cmd = ''
77
 
                try:
78
 
                    cmd = compile(ln['block'], "<web session>", 'exec');
79
 
                    
80
 
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
81
 
                    self.out = cStringIO.StringIO()
82
 
                    sys.stdout = out
83
 
                    sys.stderr = out
84
 
                    signal.alarm(5)
85
 
                    res = eval(cmd, globs, locls)
86
 
                    signal.alarm(0)
87
 
                    self.cmdQ.put({"okay":(self.out.getvalue(),res)})
88
 
                    self.curr_cmd = ''
89
 
                except Exception, exc:
90
 
                    signal.alarm(0)
91
 
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
92
 
 
93
 
    def init_state(self):
94
 
        self.globs = {}
95
 
        self.globs['__builtins__'] = globals()['__builtins__']
96
 
        self.locls = {}
97
 
        self.curr_cmd = ''
 
48
        v = (None, None, str(exc))
 
49
        web.output(cjson.encode(v))
 
50
        curr_cmd = ''
98
51
 
99
52
urls = (
100
53
    '/',            'index',
101
54
    '/index.html',  'index',
102
55
    '/(.*\.js)',    'jscript',
103
56
    '/(.*\.css)',   'style',
104
 
    '/chat',        'chat',
105
 
    '/block',       'block')
 
57
    '/chat',        'chat')
106
58
 
107
59
# The global 'magic' is the secret that the client and server share
108
60
# which is used to create and md5 digest to authenticate requests.
133
85
        web.output(file(name, "r").read())
134
86
 
135
87
class chat:
136
 
 
137
 
    def POST(self):
138
 
        inp = web.input()
139
 
 
140
 
        # Authenticate
141
 
        digest = md5.new(inp.text + magic).digest().encode('hex')
142
 
        if inp.digest != digest:
143
 
            web.output("401 Unauthorized")
144
 
            web.ctx.status = '401 Unauthorized'
145
 
            return
146
 
 
147
 
        # Okay, so the authentication succeeded,
148
 
        # so now we have the trivial matter of actually
149
 
        # executing the python....
150
 
        lineQ.put({'chat':inp.text})
151
 
        r = cmdQ.get()
152
 
        sys.__stderr__.write(cjson.encode(r) + "\n")
153
 
        web.output(cjson.encode(r))
154
 
 
155
 
class block:
156
 
 
157
 
    def POST(self):
158
 
        inp = web.input()
159
 
 
160
 
        # Authenticate
161
 
        digest = md5.new(inp.text + magic).digest().encode('hex')
162
 
        if inp.digest != digest:
163
 
            web.output("401 Unauthorized")
164
 
            web.ctx.status = '401 Unauthorized'
165
 
            return
166
 
 
167
 
        # Okay, so the authentication succeeded,
168
 
        # so now we have the trivial matter of actually
169
 
        # executing the python....
170
 
        lineQ.put({'block':inp.text})
171
 
        r = cmdQ.get()
172
 
        sys.__stderr__.write(cjson.encode(r) + "\n")
173
 
        web.output(cjson.encode(r))
174
 
 
175
 
cmdQ = Queue.Queue()
176
 
lineQ = Queue.Queue()
177
 
interpThread = PythonRunner(cmdQ, lineQ)
 
88
    def POST(self):
 
89
        inp = web.input()
 
90
        sys.stderr.write(str(inp) + "\n")
 
91
 
 
92
        # Authenticate
 
93
        digest = md5.new(inp.text + magic).digest().encode('hex')
 
94
        if inp.digest != digest:
 
95
            web.ctx.status = '401 Unauthorized'
 
96
            return
 
97
 
 
98
        # Okay, so the authentication succeeded,
 
99
        # so now we have the trivial matter of actually
 
100
        # executing the python....
 
101
        do_chat(inp.text)
178
102
 
179
103
if __name__ == "__main__":
 
104
    # FIXME jail!
180
105
    magic = sys.argv[2]
181
 
    interpThread.setDaemon(True)
182
 
    interpThread.start()
183
106
    web.run(urls, globals())