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

95 by mattgiuca
Moved some things out of www into their respective dirs, console and
1
#!/usr/bin/python
2
3
# usage:
4
#   python-console <port> <magic>
5
126 by drtomc
A basic version of the console going!
6
import sys
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
7
import web
8
import md5
9
import codeop
10
import cjson
126 by drtomc
A basic version of the console going!
11
import cgi
12
import cStringIO
13
import signal
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
14
15
globs = {}
16
globs['__builtins__'] = globals()['__builtins__']
17
locls = {}
18
compiler = codeop.CommandCompiler()
19
curr_cmd = ''
20
21
def do_chat(txt):
126 by drtomc
A basic version of the console going!
22
    global curr_cmd
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
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)
126 by drtomc
A basic version of the console going!
44
            web.output(cjson.encode(v))
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
45
            curr_cmd = ''
46
    except Exception, exc:
128 by drtomc
A couple of bug fixes.
47
        signal.alarm(0)
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
48
        v = (None, None, str(exc))
126 by drtomc
A basic version of the console going!
49
        web.output(cjson.encode(v))
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
50
        curr_cmd = ''
51
52
urls = (
53
    '/',            'index',
126 by drtomc
A basic version of the console going!
54
    '/index.html',  'index',
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
55
    '/(.*\.js)',    'jscript',
56
    '/(.*\.css)',   'style',
57
    '/chat',        'chat')
58
59
# The global 'magic' is the secret that the client and server share
60
# which is used to create and md5 digest to authenticate requests.
61
# It is assigned a real value at startup.
62
magic = ''
63
64
class index:
65
    def GET(self):
66
        inp = web.input()
67
68
        # Authenticate
69
        digest = md5.new('hello' + magic).digest().encode('hex')
70
        if inp.digest != digest:
71
            web.ctx.status = '401 Unauthorized'
72
            return
73
74
        # Okay, so the authentication succeeded,
75
        # so all we need to do is send back the static
76
        # HTML for the console app.
77
        web.output(file("index.html", "r").read())
78
79
class jscript:
80
    def GET(self, name):
81
        web.output(file(name, "r").read())
82
83
class style:
84
    def GET(self, name):
85
        web.output(file(name, "r").read())
86
87
class chat:
88
    def POST(self):
89
        inp = web.input()
126 by drtomc
A basic version of the console going!
90
        sys.stderr.write(str(inp) + "\n")
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
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....
126 by drtomc
A basic version of the console going!
101
        do_chat(inp.text)
95 by mattgiuca
Moved some things out of www into their respective dirs, console and
102
103
if __name__ == "__main__":
104
    # FIXME jail!
105
    magic = sys.argv[2]
106
    web.run(urls, globals())