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

« back to all changes in this revision

Viewing changes to console/python-console

  • Committer: matt.giuca
  • Date: 2009-01-12 01:03:59 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1077
Reverted revisions 1059 and 1069 - renaming the DB user from 'postgres' to
    'ivleuser'.
    Heart was in the right place, but currently doesn't work because postgres
    owns the relations and we've no way to correct that yet (since we manually
    run the .sql script as postgres).

    This will be reverted back again once that's solved.

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
 
        signal.alarm(0)
48
 
        v = (None, None, str(exc))
49
 
        web.output(cjson.encode(v))
50
 
        curr_cmd = ''
51
 
 
52
 
urls = (
53
 
    '/',            'index',
54
 
    '/index.html',  'index',
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()
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)
102
 
 
103
 
if __name__ == "__main__":
104
 
    # FIXME jail!
105
 
    magic = sys.argv[2]
106
 
    web.run(urls, globals())