由于未知原因提前结束脚本头

2024-09-27 17:58:58 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我通过提交表单调用下面的cgi脚本时,都会收到一个内部服务器错误,服务器日志显示以下行: 脚本头过早结束:LoopFinderRetrieval.cgi,引用者:http://loopfinder-prod-01.uit.tufts.edu/LoopFinderRetrieval.html

简而言之,cgi文件意味着进入由提交时给定的运行ID指示的文件夹,打开并读取一个名为作业状态.txt,然后根据结果执行操作。这可以是向用户返回特定的错误,也可以是向用户提供结果。据我所知,如果我忽略了以下行,就会导致我看到的错误:

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

但是这行代码是存在的,在同一台服务器上使用完全相同的PrintHeader()和PrintFooter()函数的另一个CGI脚本正在正常运行。有人能看到任何可能导致这种情况的明显错误吗?如果没有,我所读到的可能是权限问题。如果是那样的话,我就得联系管理员,把它修好,但我不想这样做,除非我知道是问题所在。谢谢。在

^{pr2}$

Tags: 文件用户服务器脚本http表单html错误
2条回答

好的,我们发现这里有代码错误,而不是权限错误。 事实上很尴尬。更正后的代码如下。第一个问题是我使用file.read()逐行读取文件。我应该使用file.readlines(),并将该行另外改为[line.rstrip('\n') for line in pre_textfile],以便删除换行符。第33行还有一个索引错误,我已经更正了。现在还不清楚的是,为什么我不能让任何类型的伐木为我工作,这样可以为几个人节省相当多的时间。在

啊!/usr/垃圾箱/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()

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

如果你的脚本崩溃了怎么办。那么就不会打印页眉了,我猜可能会发生这种错误。在

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

如果你有一点代码需要重复的话,你可能会更容易地发现你的代码。在我看来,页眉和页脚总是打印出来的,例如,也许只能打印一次。在

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

IndexError: list index out of range

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

编辑:

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

^{pr2}$

如果使用cgitb,则不需要将程序包装在try块中,实际上这两个方法可能会相互干扰。在

相关问题 更多 >

    热门问题