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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python

# usage:
#   python-console <port> <magic>

import sys
import web
import md5
import codeop
import cjson
import cgi
import cStringIO
import signal
import Queue
from threading import Thread

class StdinFromWeb(object):
    def __init__(self, cmdQ, lineQ):
        self.cmdQ = cmdQ
        self.lineQ = lineQ

    def readline(self):
        # stop the clock!
        signal.alarm(0)
        self.cmdQ.put({"input":None})
        ln = self.lineQ.get()
        if 'chat' in ln:
            # restart the clock:
            # Some of our 5 seconds may have elapsed, but never mind.
            signal.alarm(5)
            return ln['chat']

class PythonRunner(Thread):
    def __init__(self, cmdQ, lineQ):
        self.cmdQ = cmdQ
        self.lineQ = lineQ
        Thread.__init__(self)

    def run(self):
        self.init_state()
        compiler = codeop.CommandCompiler()

        while True:
            ln = self.lineQ.get()
            if 'chat' in ln:
                mode = 
                if self.curr_cmd == '':
                    self.curr_cmd = ln['chat']
                else:
                    self.curr_cmd = self.curr_cmd + '\n' + ln['chat']
                try:
                    cmd = compiler(self.curr_cmd)
                    if cmd is None:
                        # The command was incomplete,
                        # so send back a None, so the
                        # client can print a '...'
                        self.cmdQ.put({"more":None})
                    else:
                        # The command was complete,
                        # so evaluate it!
                        sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
                        self.out = cStringIO.StringIO()
                        sys.stdout = out
                        sys.stderr = out
                        signal.alarm(5)
                        res = eval(cmd, globs, locls)
                        signal.alarm(0)
                        self.cmdQ.put({"okay":(self.out.getvalue(),res)})
                        self.curr_cmd = ''
                except Exception, exc:
                    signal.alarm(0)
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})
                    self.curr_cmd = ''
            if 'block' in ln:
                # throw away a partial command.
                self.curr_cmd = ''
                try:
                    cmd = compile(ln['block'], "<web session>", 'exec');
                    
                    sys.stdin = StdinFromWeb(self.cmdQ, self.lineQ)
                    self.out = cStringIO.StringIO()
                    sys.stdout = out
                    sys.stderr = out
                    signal.alarm(5)
                    res = eval(cmd, globs, locls)
                    signal.alarm(0)
                    self.cmdQ.put({"okay":(self.out.getvalue(),res)})
                    self.curr_cmd = ''
                except Exception, exc:
                    signal.alarm(0)
                    self.cmdQ.put({"exc":(self.out.getvalue(),str(exc))})

    def init_state(self):
        self.globs = {}
        self.globs['__builtins__'] = globals()['__builtins__']
        self.locls = {}
        self.curr_cmd = ''

urls = (
    '/chat',        'chat',
    '/block',       'block')

# The global 'magic' is the secret that the client and server share
# which is used to create and md5 digest to authenticate requests.
# It is assigned a real value at startup.
magic = ''

class chat:

    def POST(self):
        inp = web.input()

        # Authenticate
        digest = md5.new(inp.text + magic).digest().encode('hex')
        if inp.digest != digest:
            web.output("401 Unauthorized")
            web.ctx.status = '401 Unauthorized'
            return

        # Okay, so the authentication succeeded,
        # so now we have the trivial matter of actually
        # executing the python....
        lineQ.put({'chat':inp.text})
        r = cmdQ.get()
        sys.__stderr__.write(cjson.encode(r) + "\n")
        web.output(cjson.encode(r))

class block:

    def POST(self):
        inp = web.input()

        # Authenticate
        digest = md5.new(inp.text + magic).digest().encode('hex')
        if inp.digest != digest:
            web.output("401 Unauthorized")
            web.ctx.status = '401 Unauthorized'
            return

        # Okay, so the authentication succeeded,
        # so now we have the trivial matter of actually
        # executing the python....
        lineQ.put({'block':inp.text})
        r = cmdQ.get()
        sys.__stderr__.write(cjson.encode(r) + "\n")
        web.output(cjson.encode(r))

cmdQ = Queue.Queue()
lineQ = Queue.Queue()
interpThread = PythonRunner(cmdQ, lineQ)

if __name__ == "__main__":
    magic = sys.argv[2]
    interpThread.setDaemon(True)
    interpThread.start()
    web.run(urls, globals())