我目前正在编写一个 CGI 脚本,其最终目标是运行 java 程序,然后重定向到不同的页面。但是,当我调用尝试运行 java 程序时,CGI 会导致 500 内部服务器错误。我已经尝试过 os.system("[command]") 和 call(["command"]) 和 subprocess.Popen("command") 并且所有三个都会导致相同的问题出现。非常感谢任何建议。
编辑此外,apache日志几乎没有给我任何信息,只说有一个无效的标题。
编辑
#!/usr/bin/python
# Import the CGI module
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>"
整个命令不存在,但我相信它是正确构建的,因为我可以将打印的命令复制出来并在终端中运行它并且它运行完美。该命令只是 java [classname] [9 args]
java代码在python代码之外运行顺利,它所做的只是生成一个.png文件和一个包含.png的.html文件。该文件的名称是 [user_id].html,因此是重定向。
如果仅注释掉进程命令,则不会发生内部服务器错误。
编辑最终放弃并转向 php 执行此脚本。感谢所有的帮助!