【发布时间】:2011-03-28 08:30:02
【问题描述】:
我目前正在编写一个 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,因此是重定向。
如果仅注释掉进程命令,则不会发生内部服务器错误不会。
EDIT 最终放弃并转向 php 来执行此脚本。感谢大家的帮助!
【问题讨论】:
-
你能发布你的代码吗?看起来您的代码中有错误,或者您的代码没有首先生成 http 标头。
-
只是其中的一部分。我会尽力而为。
标签: java python cgi popen internal-server-error