内部服务器错误 - Python CGI

发布于 2024-10-27 09:09:18 字数 1631 浏览 0 评论 0原文

我目前正在编写一个 CGI 脚本,其最终目标是运行 java 程序,然后重定向到不同的页面。但是,当我调用 try 运行 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 来执行此脚本。感谢您的帮助!

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 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>"

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘曦 2024-11-03 09:09:19
## This is where the problem exists
#os.system("ls -l")
#call(["ls", "-l"])
#p = subprocess.Popen(command)
## End of Problem
  1. 重点关注subprocess.Popen。丢弃所有其他的。永久地。它们都是坏主意。

  2. 如果 subprocess.Popen(command) 确实作为独立的短 Python 脚本工作,那么您必须调查 Apache CGI-bin 用户名是否具有运行此脚本所需的权限。记住。 Apache 不会像你一样运行。它作为具有单独权限的单独“人”运行。

  3. 您的 java 命令可能正在将数据喷射到 sys.stdout 和/或 sys.stderr 中。如果是这样,该输出将发送到 Apache。理想情况下,一些内容已经通过 sys.stdout 发送到 Apache,这是一个正确的标头,一切都很好。

    但是,由于缓冲方式的原因,Apache 很有可能没有看到可用的响应,并且 java 输出很混乱。

我建议您改用 mod_wsgi 而不是 CGI,因为它更容易控制。

我还建议您捕获文件中的子进程输出,然后将该文件复制到 sys.stdout,以便 Apache 不会被 java 子进程混淆。

## This is where the problem exists
#os.system("ls -l")
#call(["ls", "-l"])
#p = subprocess.Popen(command)
## End of Problem
  1. Focus on subprocess.Popen. Discard all others. Permanently. They're all bad ideas.

  2. 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.

  3. 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.

提笔落墨 2024-11-03 09:09:19

您是否尝试过:

import cgitb

cgitb.enable()

在浏览器中获取更有意义的错误 - python 错误消息

从服务器上的命令行运行脚本以查看 python 是否返回错误,如果是,是否有助于诊断问题?

或者

从 python 脚本将 java 调用打印为 html 输出?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文