Internal Server Error - Python CGI
I am currently writing a CGI script that has an end goal of running a java program and then redirecting to a different page. However, the CGI causes a 500 internal server error when I call try to run the java program. I've tried os.system("[command]") and call(["command"]) and subprocess.Popen("command") and all three cause the same issue to crop up. Any suggestions are greatly appreciated.
Edit Also, the apache log gives me next to no information saying only that there is an invalid header.
Edit
#!/usr/bin/python
# Import the CGI modu开发者_如何学JAVAle
import cgi
import random
import os
import sys
import re
import subprocess
# Required header that tells the browser how to render the HTML.
print "Content-Type: text/html\n\n"
form = cgi.FieldStorage()
userID = random.random()
print "<html>"
print "<title>Results</title>"
print "<body>"
command = "java [classname] "
user_id = random.randint(0, sys.maxint)
command += str(user_id) + " "
command += re.sub(r'\s', '', form['key0'].value) + " "
command += form['key1'].value + " "
command += form['key2'].value + " "
command += form['key3'].value + " " if form.has_key('key3') else "0 "
## This is where the problem exists
#os.system("ls -l")
#call(["ls", "-l"])
#p = subprocess.Popen(command)
## End of Problem
print command
print "<br>"
print "Redirecting..."
print "<meta http-equiv='refresh' content='0;url=",
print "http://192.168.1.41/Path/",
print user_id,
print ".html' />"
print "</body>"
print "</html>"
the entire command is not there, but I believe it is built correctly because I can copy the printed command out and run it in a terminal and it runs perfectly. The command is simply java [classname] [9 args]
the java code runs smoothly outside of the python code and all it does is generate a .png file and a .html file containing the .png. The name of the file is [user_id].html, hence the redirect.
the internal server error does not occur if only the process commands are commented out.
EDIT Ended up giving up and moving to php to execute this script. Thanks for all the help!
## This is where the problem exists
#os.system("ls -l")
#call(["ls", "-l"])
#p = subprocess.Popen(command)
## End of Problem
Focus on
subprocess.Popen
. Discard all others. Permanently. They're all bad ideas.If
subprocess.Popen(command)
really does work as a stand-alone short Python script, then you have to investigate whether or not the Apache CGI-bin username has the required privileges to run this. Remember. Apache doesn't run as you. It runs as a separate "person" with separate privileges.Your java command is probably spewing data into sys.stdout and or sys.stderr. If so, that output goes to Apache. Ideally, some stuff has already gone to Apache via sys.stdout which is a proper header and everything's good.
However, because of the way things are buffered, there's a small chance that Apache has seen no useable response and the java output is confusing.
I'd suggest that you switch to mod_wsgi
instead of CGI because it's easier to control.
I'd also suggest that you capture the subprocess output in a file and then copy that file to sys.stdout so that Apache can't be confused by the java subprocess.
Have you tried:
import cgitb
cgitb.enable()
to get a more meaningful error - python error message in your browser
or
running your script from the command line on your server to see if python is returning an error, and if so, if it helps to diagnose the problem?
or
printing your java call as html output from your python script?
精彩评论