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

« back to all changes in this revision

Viewing changes to www/console/python-console

  • Committer: mattgiuca
  • Date: 2007-12-20 03:17:34 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:95
Moved some things out of www into their respective dirs, console and
trampoline.

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 web
7
 
import md5
8
 
import codeop
9
 
import cjson
10
 
 
11
 
globs = {}
12
 
globs['__builtins__'] = globals()['__builtins__']
13
 
locls = {}
14
 
compiler = codeop.CommandCompiler()
15
 
curr_cmd = ''
16
 
 
17
 
def do_chat(txt):
18
 
    if curr_cmd == '':
19
 
        curr_cmd = txt
20
 
    else:
21
 
        curr_cmd = curr_cmd + '\n' + txt
22
 
    try:
23
 
        cmd = compiler(curr_cmd)
24
 
        if cmd is None:
25
 
            # The command was incomplete,
26
 
            # so send back a None, so the
27
 
            # client can print a '...'
28
 
            web.output(cjson.encode(None))
29
 
        else:
30
 
            # The command was complete,
31
 
            # so evaluate it!
32
 
            out = cStringIO.StringIO()
33
 
            sys.stdout = out
34
 
            sys.stderr = out
35
 
            signal.alarm(5)
36
 
            res = eval(cmd, globs, locls)
37
 
            signal.alarm(0)
38
 
            v = (out.getvalue(), res, None)
39
 
            web.output(json.encode(v))
40
 
            curr_cmd = ''
41
 
    except Exception, exc:
42
 
        v = (None, None, str(exc))
43
 
        web.output(json.encode(v))
44
 
        curr_cmd = ''
45
 
 
46
 
urls = (
47
 
    '/',            'index',
48
 
    '/(.*\.js)',    'jscript',
49
 
    '/(.*\.css)',   'style',
50
 
    '/chat',        'chat')
51
 
 
52
 
# The global 'magic' is the secret that the client and server share
53
 
# which is used to create and md5 digest to authenticate requests.
54
 
# It is assigned a real value at startup.
55
 
magic = ''
56
 
 
57
 
class index:
58
 
    def GET(self):
59
 
        inp = web.input()
60
 
 
61
 
        # Authenticate
62
 
        digest = md5.new('hello' + magic).digest().encode('hex')
63
 
        if inp.digest != digest:
64
 
            web.ctx.status = '401 Unauthorized'
65
 
            return
66
 
 
67
 
        # Okay, so the authentication succeeded,
68
 
        # so all we need to do is send back the static
69
 
        # HTML for the console app.
70
 
        web.output(file("index.html", "r").read())
71
 
 
72
 
class jscript:
73
 
    def GET(self, name):
74
 
        web.output(file(name, "r").read())
75
 
 
76
 
class style:
77
 
    def GET(self, name):
78
 
        web.output(file(name, "r").read())
79
 
 
80
 
class chat:
81
 
    def POST(self):
82
 
        inp = web.input()
83
 
 
84
 
        # Authenticate
85
 
        digest = md5.new(inp.text + magic).digest().encode('hex')
86
 
        if inp.digest != digest:
87
 
            web.ctx.status = '401 Unauthorized'
88
 
            return
89
 
 
90
 
        # Okay, so the authentication succeeded,
91
 
        # so now we have the trivial matter of actually
92
 
        # executing the python....
93
 
        web.output(do_chat(inp.text))
94
 
 
95
 
if __name__ == "__main__":
96
 
    # FIXME jail!
97
 
    magic = sys.argv[2]
98
 
    web.run(urls, globals())