2016-11-29 82 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,那麼提供內容類型的標題幾乎肯定不是必需的。