过早结束脚本?

时间:2016-11-29 02:30:54

标签: python mysql amazon-web-services

我一直遇到过早结束脚本错误和追溯错误的问题。

以下代码为modifyStudent.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import cgi, cgitb
import dbConnect
import menuHelper

cgitb.enable()

formData = cgi.FieldStorage()

firstName = menuHelper.getNameValue(formData, "firstName")
lastName = menuHelper.getNameValue(formData, "lastName")

# Tell the browser what kind of data to expect 
print ("Content-type: text/html\n")

print("<h2>Modify Student</h2>")

menuHelper.printMenuLink()

print("<br><br>")

isUpdate = dbConnect.getStudentData(lastName, firstName)
print("isUpdate=", isUpdate)

print('<center><form method="post" action="modifyStudentHandler.py">',   
        '<input type="hidden" name="isUpdate" value=' + str(isUpdate) + '>', 
        'First Name:', '<input type="text" name="firstName" value=' + firstName + ' readonly>', "<br>" 
        'Last Name: ', '<input type="text" name="lastName" value=' + lastName + ' readonly>', "<br>",
        # Fields for the grades.  
        'Hw 1: ', '<input type="numbers" name="hw1">', "<br>",
        'Hw 2: ', '<input type="numbers" name="hw2">', "<br>",
        'Hw 3: ', '<input type="numbers" name="hw3">', "<br>",
        'Midterm: ', '<input type="numbers" name="midterm">', "<br>",
        'Final: ', '<input type="numbers" name="final">', "<br>",
        # Submit button 
        '<input type="submit" value="Save">',
      '</form></center>',
     '</center>');

  dbConnect.closeConnection()

我目前正在运行一个网络应用,我正在尝试为项目添加此功能,但是我收到以下错误:

[error] [client 24.169.14.133] Premature end of script headers: modifyStudent.py, referer: http://34.193.0.192/cgi-bin/menu.py
[error] [client 24.169.14.133] Traceback (most recent call last):, referer: http://34.193.0.192/cgi-bin/menu.py
[error] [client 24.169.14.133]   File "/var/www/devApp/cgi-bin/modifyStudent.py", line 7, in <module>, referer: http://34.193.0.192/cgi-bin/menu.py
[error] [client 24.169.14.133]     import menuHelper.py, referer: http://34.193.0.192/cgi-bin/menu.py

为什么我会收到这些错误?

1 个答案:

答案 0 :(得分:0)

您的问题与HTTP的工作方式有关。标准是输入应该是:

header lines
blank line
HTML Content

你的行:

print ("Content-type: text/html\n")

是标题,然后在有效负载之前没有空白行。

因此你可能想要:

print ("Content-type: text/html\n\n") # note the extra \n
print("<h2>Modify Student</h2>")

所以你的输出变成了:

Content-type: text/html

<h2>Modify Student</h2>
.... other HTML.

除此之外,你的HTML形成得很糟糕,你应该真的有:

<html>
   <head></head>
   <body>your HTML goes here</body>
</html>

作为最小值。如果您将有效负载作为正确的,格式正确的HTML,那么提供内容类型的标题几乎肯定不是必需的。