我一直遇到脚本错误和回溯错误的过早终结的问题。
下面的代码是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()
我当前正在运行一个Web应用程序,试图为项目添加此功能,但是出现以下错误:
[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
为什么会出现这些错误?
最佳答案
您的问题与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,那么几乎可以肯定不需要提供内容类型的标题。