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

« back to all changes in this revision

Viewing changes to src/apps/server/__init__.py

  • Committer: mattgiuca
  • Date: 2007-12-14 04:53:50 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:61
dispatch/request: added read method.
server: Execute CGI with popen. But it doesn't work :(

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import functools
31
31
import mimetypes
32
32
import os
 
33
import subprocess
33
34
 
34
35
def handle(req):
35
36
    """Handler for the Server application which serves pages."""
40
41
    if type is None:
41
42
        type = 'text/plain'
42
43
 
43
 
    req.content_type = type
44
 
 
45
44
    req.write_html_head_foot = False
46
45
 
47
46
    # Get the username of the student whose work we are browsing, and the path
57
56
    if type in executable_types:
58
57
        return executable_types[type](path, req)
59
58
    else:
 
59
        req.content_type = type
60
60
        req.sendfile(path)
61
61
 
62
62
def execute_cgi(filename, studentprog, req):
71
71
    the HTTP body on stdin. It shall receive the CGI environment variables to
72
72
    its environment.
73
73
    """
74
 
    # TEMP
75
 
    req.write("execute_cgi(%s, %s, req)\n" % (filename, studentprog))
 
74
 
 
75
    # Get the student program's directory and execute it from that context.
 
76
    slashloc = studentprog.rfind(os.sep)
 
77
    if slashloc >= 0:
 
78
        progdir = studentprog[0:slashloc]
 
79
    else:
 
80
        progdir = "/"
 
81
 
 
82
    # TODO: Don't create a file if the body length is known to be 0
 
83
    # Write the HTTP body to a temporary file so it can be passed as a *real*
 
84
    # file to popen.
 
85
    f = os.tmpfile()
 
86
    body = req.read()
 
87
    if body is not None:
 
88
        f.write(body)
 
89
        f.flush()
 
90
        f.seek(0)       # Rewind, for reading
 
91
 
 
92
    pid = subprocess.Popen([studentprog], executable=filename,
 
93
        stdin=f, stdout=subprocess.PIPE, cwd=progdir)
 
94
 
 
95
    # Read from the process's stdout into req
 
96
    # FIXME: Efficiency
 
97
    response = pid.stdout.read()
 
98
    req.write(response)
 
99
 
 
100
# Mapping of mime types to executables
76
101
 
77
102
executable_types = {
78
103
    'text/x-python'