脚本过早结束 headers 原因不明

Premature end of script headers for unknown reason

每当我通过向其提交表单来调用下面的 cgi 脚本时,我都会收到内部服务器错误并且服务器日志显示以下行: 脚本过早结束 headers: LoopFinderRetrieval.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()

错误信息够模糊,但它基本上意味着输出在headers结束之前结束。就像你说的。

如果你的脚本崩溃了怎么办。然后不会打印headers,我猜可能会出现这个错误。

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

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

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

IndexError: list index out of range

但这只是猜测,适当的日志记录会让您知道。

编辑:

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

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

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

好的,事实证明我们这里有代码错误,而不是权限错误。 事实上,相当令人尴尬。更正后的代码如下。第一个问题是我使用 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()