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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: drtomc
  • Date: 2008-01-02 04:45:20 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:126
A basic version of the console going!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# usage:
 
4
#   python-console <port> <magic>
 
5
 
 
6
import sys
 
7
import web
 
8
import md5
 
9
import codeop
 
10
import cjson
 
11
import cgi
 
12
import cStringIO
 
13
import signal
 
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:
 
47
        v = (None, None, str(exc))
 
48
        web.output(cjson.encode(v))
 
49
        curr_cmd = ''
 
50
 
 
51
urls = (
 
52
    '/',            'index',
 
53
    '/index.html',  'index',
 
54
    '/(.*\.js)',    'jscript',
 
55
    '/(.*\.css)',   'style',
 
56
    '/chat',        'chat')
 
57
 
 
58
# The global 'magic' is the secret that the client and server share
 
59
# which is used to create and md5 digest to authenticate requests.
 
60
# It is assigned a real value at startup.
 
61
magic = ''
 
62
 
 
63
class index:
 
64
    def GET(self):
 
65
        inp = web.input()
 
66
 
 
67
        # Authenticate
 
68
        digest = md5.new('hello' + magic).digest().encode('hex')
 
69
        if inp.digest != digest:
 
70
            web.ctx.status = '401 Unauthorized'
 
71
            return
 
72
 
 
73
        # Okay, so the authentication succeeded,
 
74
        # so all we need to do is send back the static
 
75
        # HTML for the console app.
 
76
        web.output(file("index.html", "r").read())
 
77
 
 
78
class jscript:
 
79
    def GET(self, name):
 
80
        web.output(file(name, "r").read())
 
81
 
 
82
class style:
 
83
    def GET(self, name):
 
84
        web.output(file(name, "r").read())
 
85
 
 
86
class chat:
 
87
    def POST(self):
 
88
        inp = web.input()
 
89
        sys.stderr.write(str(inp) + "\n")
 
90
 
 
91
        # Authenticate
 
92
        digest = md5.new(inp.text + magic).digest().encode('hex')
 
93
        if inp.digest != digest:
 
94
            web.ctx.status = '401 Unauthorized'
 
95
            return
 
96
 
 
97
        # Okay, so the authentication succeeded,
 
98
        # so now we have the trivial matter of actually
 
99
        # executing the python....
 
100
        do_chat(inp.text)
 
101
 
 
102
if __name__ == "__main__":
 
103
    # FIXME jail!
 
104
    magic = sys.argv[2]
 
105
    web.run(urls, globals())