由于未知原因,脚本标头过早结束

时间:2015-06-30 15:06:20

标签: python web cgi python-2.6

每当我通过向其提交表单来调用下面的cgi脚本时,我收到内部服务器错误,服务器日志显示该行: 脚本标题的过早结束:LoopF​​inderRetrieval.cgi,referer:http://loopfinder-prod-01.uit.tufts.edu/LoopFinderRetrieval.html

简而言之,cgi文件意味着转到run-ID指示的文件夹,在提交时给出,打开并读取名为JobStatus.txt的文件,然后根据结果执行操作。这可以是向用户返回特定错误,也可以向他们提供结果。据我了解,如果我是,例如省略了行,那么我所看到的错误就会引起:

"Content-type:text/html\r\n\r\n"

但该行存在,并且在同一服务器上使用完全相同的PrintHeader()和PrintFooter()函数的另一个CGI脚本正在运行而没有错误。任何人都可以看到可能导致此问题的明显错误吗?如果没有,我读过的内容表明这可能是权限问题。在那种情况下,我将不得不联系管理员并修复它,但除非我知道这是问题,否则我不想这样做。感谢。

#!/usr/bin/python2.6

# Import modules for CGI handling 
import cgi, cgitb
import os 
cgitb.enable()

#Functions to automatically print HTML headers and footers.
def PrintHeader():
    print "Content-type:text/html\r\n\r\n"
    print "<html>"
    print "<head>"
    print "<title>LoopFinder Running</title>"
    print "</head>"
    print "<body>"
def PrintFooter():
    print "</body>"
    print "</html>"

# Create instance of FieldStorage 
form = cgi.FieldStorage() 
ID_Number = form.getvalue('IDNum')

with open('/loopfinder_data/RunData/%s/JobStatus.txt' % ID_Number, 'r') as pre_textfile:
    textfile = pre_textfile.read()
    if textfile[0] == 'Running':
        PrintHeader()
        print '<h2>Your run is not complete. Please check back later.</h2>'
        PrintFooter()
    if textfile[0] == 'Stopped':
        PDBID = textfile[3]
        if textfile[1] == 'PDBError':
            PrintHeader()
            print '<h2>We were unable to download the PBDID you entered, which was %s</h2>' % PDBID
            print '<h2>Please check that this PDBID exists before trying again.</h2>'
            PrintFooter()
        elif textfile[1] == 'ChainCountError':
            PrintHeader()
            print '<h2>We were unable to either download or open the PBDID you entered, which was %s</h2>' % PDBID
            print '<h2>Please check that this PDBID exists before trying again.</h2>'
            PrintFooter()       
        elif textfile[1] == 'SingleChainError':
            PrintHeader()
            print '<h2>It appears that your PDB structure of interest contains only one chain.</h2>'
            print '<h2>LoopFinder requires a multi-chain interface from which to calculate energy values.</h2>'
            PrintFooter()
        elif textfile[1] == 'LoopFinderError':
            PrintHeader()
            print '<h2>LoopFinder experienced an unknown error while analyzing your PDB file.</h2>'
            print '<h2>Leave a comment including your run ID and we will try to solve this issue.</h2>'
            PrintFooter()   
        elif textfile[1] == 'PyRosettaError':
            PrintHeader()
            print '<h2>PyRosetta experienced an unknown error while analyzing your PDB file.</h2>'
            print '<h2>Leave a comment including your run ID and we will try to solve this issue.</h2>'
            PrintFooter()                   
    if textfile[0] == 'Completed':
        PrintHeader()
        print '<a href="http://<url_redacted>/loopfinder_data/RunData/%s/results/%s_Results.zip">\
Click here to download your results.</a>' % (ID_Number,ID_Number)
        PrintFooter()

2 个答案:

答案 0 :(得分:1)

错误消息足够模糊,但它基本上意味着输出在标头结束之前结束。就像你说的那样。

如果您的脚本崩溃了怎么办?然后没有打印标题,我猜这个错误可能会发生。

在脚本中添加一些logging,以便更好地了解实际发生的情况。您可以将整个脚本包装在try块中,并记录任何异常,例如。

你有很多重复的代码,也许如果你稍微重构一下你的脚本,那么bug就会更容易找到。在我看来,总是打印页眉和页脚,例如,它可能只能完成一次。

我的猜测是,当文件太短时,脚本会崩溃。当你点击一行textfile[1] == something并且文件只有一行时,你会得到以下异常:

IndexError: list index out of range

但这只是猜测,正确的记录会让你知道。

修改

我发现你正在使用cgitb。也许使用此模块的日志记录。尝试禁用浏览器输出并将任何异常发送到日志文件。将cgitb.enable()更改为:

cgitb.enable(display=0, logdir='/tmp/')

如果您使用try,则无法将程序包装在cgitb块中,实际上这两种方法可能会产生干扰。

答案 1 :(得分:0)

好的,事实证明我们这里有代码错误,而不是权限错误。 事实上相当尴尬。更正后的代码如下。第一个问题是我使用file.read()尝试逐行读取文件。我本来应该使用file.readlines(),并且还将行更改为[line.rstrip('\n') for line in pre_textfile]以删除换行符。第33行还有一个索引错误,我已经纠正了。目前尚不清楚的是为什么我无法为我工作任何类型的伐木工作,这会为几个人节省相当多的时间。

!在/ usr / bin中/ python2.6的

# Import modules for CGI handling 
import cgi, cgitb
import os 
cgitb.enable()

#Functions to automatically print HTML headers and footers.
def PrintHeader():
    print "Content-type:text/html\r\n\r\n"
    print "<html>"
    print "<head>"
    print "<title>LoopFinder Running</title>"
    print "</head>"
    print "<body>"
def PrintFooter():
    print "</body>"
    print "</html>"

# Create instance of FieldStorage 
form = cgi.FieldStorage() 
ID_Number = form.getvalue('IDNum')

with open('/loopfinder_data/RunData/%s/JobStatus.txt' % ID_Number, 'r') as pre_textfile:
    textfile = [line.rstrip('\n') for line in pre_textfile]
    if textfile[0] == 'Running':
        PrintHeader()
        print '<h2>Your run is not complete. Please check back later.</h2>'
        PrintFooter()
    if textfile[0] == 'Stopped':
        PDBID = textfile[2]
        if textfile[1] == 'PDBError':
            PrintHeader()
            print '<h2>We were unable to download the PBDID you entered, which was %s</h2>' % PDBID
            print '<h2>Please check that this PDBID exists before trying again.</h2>'
            PrintFooter()
        elif textfile[1] == 'ChainCountError':
            PrintHeader()
            print '<h2>We were unable to either download or open the PBDID you entered, which was %s</h2>' % PDBID
            print '<h2>Please check that this PDBID exists before trying again.</h2>'
            PrintFooter()       
        elif textfile[1] == 'SingleChainError':
            PrintHeader()
            print '<h2>It appears that your PDB structure of interest contains only one chain.</h2>'
            print '<h2>LoopFinder requires a multi-chain interface from which to calculate energy values.</h2>'
            PrintFooter()
        elif textfile[1] == 'LoopFinderError':
            PrintHeader()
            print '<h2>LoopFinder experienced an unknown error while analyzing your PDB file.</h2>'
            print '<h2>Leave a comment including your run ID and we will try to solve this issue.</h2>'
            PrintFooter()   
        elif textfile[1] == 'PyRosettaError':
            PrintHeader()
            print '<h2>PyRosetta experienced an unknown error while analyzing your PDB file.</h2>'
            print '<h2>Leave a comment including your run ID and we will try to solve this issue.</h2>'
            PrintFooter()                   
    if textfile[0] == 'Completed':
        PrintHeader()
        print '<a href="http://<url_redacted>/loopfinder_data/RunData/%s/results/%s_Results.zip">\
Click here to download your results.</a>' % (ID_Number,ID_Number)
        PrintFooter()