mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
CGIHTTPRequestHandler.run_cgi() to use subprocess for Non Unix platforms. Fix
based on Issue1235.
This commit is contained in:
parent
47dded644f
commit
3a145a18c8
1 changed files with 23 additions and 54 deletions
|
@ -253,75 +253,44 @@ class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
||||||
self.server.handle_error(self.request, self.client_address)
|
self.server.handle_error(self.request, self.client_address)
|
||||||
os._exit(127)
|
os._exit(127)
|
||||||
|
|
||||||
elif self.have_popen2 or self.have_popen3:
|
else:
|
||||||
# Windows -- use popen2 or popen3 to create a subprocess
|
# Non Unix - use subprocess
|
||||||
import shutil
|
import subprocess
|
||||||
if self.have_popen3:
|
cmdline = [scriptfile]
|
||||||
popenx = os.popen3
|
|
||||||
else:
|
|
||||||
popenx = os.popen2
|
|
||||||
cmdline = scriptfile
|
|
||||||
if self.is_python(scriptfile):
|
if self.is_python(scriptfile):
|
||||||
interp = sys.executable
|
interp = sys.executable
|
||||||
if interp.lower().endswith("w.exe"):
|
if interp.lower().endswith("w.exe"):
|
||||||
# On Windows, use python.exe, not pythonw.exe
|
# On Windows, use python.exe, not pythonw.exe
|
||||||
interp = interp[:-5] + interp[-4:]
|
interp = interp[:-5] + interp[-4:]
|
||||||
cmdline = "%s -u %s" % (interp, cmdline)
|
cmdline = [interp, '-u'] + cmdline
|
||||||
if '=' not in query and '"' not in query:
|
if '=' not in query:
|
||||||
cmdline = '%s "%s"' % (cmdline, query)
|
cmdline.append(query)
|
||||||
self.log_message("command: %s", cmdline)
|
|
||||||
|
self.log_message("command: %s", subprocess.list2cmdline(cmdline))
|
||||||
try:
|
try:
|
||||||
nbytes = int(length)
|
nbytes = int(length)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
nbytes = 0
|
nbytes = 0
|
||||||
files = popenx(cmdline, 'b')
|
files = subprocess.Popen(cmdline,
|
||||||
fi = files[0]
|
stdin = subprocess.PIPE,
|
||||||
fo = files[1]
|
stdout = subprocess.PIPE,
|
||||||
if self.have_popen3:
|
stderr = subprocess.PIPE
|
||||||
fe = files[2]
|
)
|
||||||
if self.command.lower() == "post" and nbytes > 0:
|
if self.command.lower() == "post" and nbytes > 0:
|
||||||
data = self.rfile.read(nbytes)
|
data = self.rfile.read(nbytes)
|
||||||
fi.write(data)
|
else:
|
||||||
|
data = None
|
||||||
# throw away additional data [see bug #427345]
|
# throw away additional data [see bug #427345]
|
||||||
while select.select([self.rfile._sock], [], [], 0)[0]:
|
while select.select([self.rfile._sock], [], [], 0)[0]:
|
||||||
if not self.rfile._sock.recv(1):
|
if not self.rfile._sock.recv(1):
|
||||||
break
|
break
|
||||||
fi.close()
|
stdout, stderr = p.communicate(data)
|
||||||
shutil.copyfileobj(fo, self.wfile)
|
self.wfile.write(stdout)
|
||||||
if self.have_popen3:
|
if stderr:
|
||||||
errors = fe.read()
|
self.log_error('%s', stderr)
|
||||||
fe.close()
|
status = p.returncode
|
||||||
if errors:
|
if status:
|
||||||
self.log_error('%s', errors)
|
self.log_error("CGI script exit status %#x", status)
|
||||||
sts = fo.close()
|
|
||||||
if sts:
|
|
||||||
self.log_error("CGI script exit status %#x", sts)
|
|
||||||
else:
|
|
||||||
self.log_message("CGI script exited OK")
|
|
||||||
|
|
||||||
else:
|
|
||||||
# Other O.S. -- execute script in this process
|
|
||||||
save_argv = sys.argv
|
|
||||||
save_stdin = sys.stdin
|
|
||||||
save_stdout = sys.stdout
|
|
||||||
save_stderr = sys.stderr
|
|
||||||
try:
|
|
||||||
save_cwd = os.getcwd()
|
|
||||||
try:
|
|
||||||
sys.argv = [scriptfile]
|
|
||||||
if '=' not in decoded_query:
|
|
||||||
sys.argv.append(decoded_query)
|
|
||||||
sys.stdout = self.wfile
|
|
||||||
sys.stdin = self.rfile
|
|
||||||
execfile(scriptfile, {"__name__": "__main__"})
|
|
||||||
finally:
|
|
||||||
sys.argv = save_argv
|
|
||||||
sys.stdin = save_stdin
|
|
||||||
sys.stdout = save_stdout
|
|
||||||
sys.stderr = save_stderr
|
|
||||||
os.chdir(save_cwd)
|
|
||||||
except SystemExit, sts:
|
|
||||||
self.log_error("CGI script exit status %s", str(sts))
|
|
||||||
else:
|
else:
|
||||||
self.log_message("CGI script exited OK")
|
self.log_message("CGI script exited OK")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue