2016-11-29 77 views
0

我一直有脚本错误和回溯错误提前结束的问题。脚本提前结束?

下面的代码是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 

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

+0

这是如此90年代。使用像django这样的python框架 – e4c5

回答

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,那么提供内容类型的标题几乎肯定不是必需的。