Python CGI: 500内部服务器错误,在哪里查找我的错误?

0 投票
1 回答
691 浏览
提问于 2025-04-18 01:45

我在测试我的代码,做了很多修改,但每次都出现“500内部服务器错误”。我不知道自己哪里出错了,也不知道在哪里能找到更详细的错误描述。

谢谢。

编辑:在下面添加了代码。

这是我的代码:

#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

new = open("survey.ssv", "r+")
new.write(form.getvalue("surveyQuestion")+"\n")
new.seek(0)
lines = new.readlines()
i = 0

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Create-a-Survey!</title>"
print "</head>"
print "<body bgcolor='white' text='black'>"
print "<center>"
print "<h1>Create Survey Page</h>"
print "</center>"
print "To create a survey first input a title in the 'Survey Title' text box. To add questions,  write a question in the 'Survey Question' text box and click the 'Add' button. If you want to create a new survey, write a new survey title and click 'New'. The 'Done' button will end the survey creation and return you to the welcome page."
print "<br><br>"

for line in lines:
    if i == 0:
        print "Current Survey Title:<br>%s" % line
        print "<br><br>"
        print '<form action="newSurvey.py" method="post">'
        print 'Survey Title:<br><input type="text" name="surveyTitle">'
        print '<input type="submit" name="decision" value="New">'
        print '</form>'
        print '<br><br>'

    else:
        print "Question %d:<br>%s" % i, % line
        print '<br><br>'
    i = i + 1
new.close()
print '<form action="addQuestion.py" method="post">'
print 'Survey Question:<br><input type="text" name="surveyQuestion">'
print '<br><br>'
print '<input type="submit" name="decision" value="Add">'
print '</form>'
print '<form action="doneSurvey.py" method="post">'
print '<input type="submit" name="decision" value="Done">'
print '</form>'
print '<a href="welcome.html">Back to welcome page</a>'
print '</body>'
print '</html>'

这是目前能正常工作的代码,也是我在运行上面的代码之前执行的:

#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()   # enable debugging mode

form = cgi.FieldStorage()

new = open("survey.ssv", "r+")
new.write(form.getvalue("surveyQuestion")+"\n")
lines = new.readlines()
i = 0

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Create-a-Survey!</title>"
print "</head>"
print "<body bgcolor='white' text='black'>"
print "<center>"
print "<h1>Create Survey Page</h>"
print "</center>"
print "To create a survey first input a title in the 'Survey Title' text box. To add questions,  write a question in the 'Survey Question' text box and click the 'Add' button. If you want to create a new survey, write a new survey title and click 'New'. The 'Done' button will end the survey creation and return you to the welcome page."
print "<br><br>"

for line in lines:
    if i == 0:
        print "Current Survey Title:<br>%s" % line
        print "<br><br>"
        print '<form action="newSurvey.py" method="post">'
        print 'Survey Title:<br><input type="text" name="surveyTitle">'
        print '<input type="submit" name="decision" value="New">'
        print '</form>'
        print '<br><br>'

    else:
        print "Question %d:<br>%s" % i, % line
        print '<br><br>'
    i = i + 1
new.close()
print '<form action="addQuestion.py" method="post">'
print 'Survey Question:<br><input type="text" name="surveyQuestion">'
print '<br><br>'
print '<input type="submit" name="decision" value="Add">'
print '</form>'
print '<form action="doneSurvey.py" method="post">'
print '<input type="submit" name="decision" value="Done">'
print '</form>'
print '<a href="welcome.html">Back to welcome page</a>'
print '</body>'
print '</html>'

1 个回答

1

这一行不是有效的Python代码:

print "Question %d:<br>%s" % i, % line

如果你想把两个值结合在一起,可以使用一个元组:

print "Question %d:<br>%s" % (i, line)

撰写回答