Python CGI:500内部服务器错误,哪里可以找到我做错了什么?

Python CGI:500内部服务器错误,哪里可以找到我做错了什么?,python,cgi,internal-server-error,Python,Cgi,Internal Server Error,因此,我尝试测试我的代码,并做了很多更改,但每次都会出现“500内部服务器错误”。我不明白我做错了什么。我也不知道在哪里可以找到更好的错误描述 谢谢 编辑:在底部添加代码 这是我的代码: #!/usr/bin/python import cgi import cgitb; cgitb.enable() form = cgi.FieldStorage() new = open("survey.ssv", "r+") new.write(form.getvalue("surveyQuestion

因此,我尝试测试我的代码,并做了很多更改,但每次都会出现“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
导入cgi
进口cgib;cgib.enable()
form=cgi.FieldStorage()
新建=打开(“survey.ssv”、“r+”)
new.write(form.getvalue(“surveyQuestion”)+“\n”)
新搜索(0)
lines=new.readlines()
i=0
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
打印“创建调查!”
打印“”
打印“”
打印“”
打印“创建调查页”
打印“”
打印“要创建调查,请首先在“调查标题”文本框中输入标题。要添加问题,请在“调查问题”文本框中编写问题,然后单击“添加”按钮。如果要创建新调查,请编写新调查标题,然后单击“新建”。“完成”按钮将结束调查创建并返回欢迎页面
打印“

” 对于行中的行: 如果i==0: 打印“当前调查标题:
%s”%行 打印“

” 打印“ 打印“调查标题:
” 打印“ 打印“ 打印“

” 其他: 打印“问题%d:
%s”%i,%line 打印“

” i=i+1 new.close() 打印“ 打印“调查问题:
” 打印“

” 打印“ 打印“ 打印“ 打印“ 打印“ 打印“ 打印“ 打印“
这是当前正在运行的代码,也是我在上面代码之前运行的代码:

#!/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>'
#/usr/bin/python
导入cgi
进口cgib;cgib.enable()#启用调试模式
form=cgi.FieldStorage()
新建=打开(“survey.ssv”、“r+”)
new.write(form.getvalue(“surveyQuestion”)+“\n”)
lines=new.readlines()
i=0
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
打印“创建调查!”
打印“”
打印“”
打印“”
打印“创建调查页”
打印“”
打印“要创建调查,请首先在“调查标题”文本框中输入标题。要添加问题,请在“调查问题”文本框中编写问题,然后单击“添加”按钮。如果要创建新调查,请编写新调查标题,然后单击“新建”。“完成”按钮将结束调查创建并返回欢迎页面
打印“

” 对于行中的行: 如果i==0: 打印“当前调查标题:
%s”%行 打印“

” 打印“ 打印“调查标题:
” 打印“ 打印“ 打印“

” 其他: 打印“问题%d:
%s”%i,%line 打印“

” i=i+1 new.close() 打印“ 打印“调查问题:
” 打印“

” 打印“ 打印“ 打印“ 打印“ 打印“ 打印“ 打印“ 打印“
此行无效:

print "Question %d:<br>%s" % i, % line
打印“问题%d:
%s”%i,%line
如果要插值两个值,请使用元组:

print "Question %d:<br>%s" % (i, line)
打印“问题%d:
%s”%(i,第行)
写入
r+
文件,然后从中读取将导致行列表为空;首先查找开始。至于错误,这取决于您的web服务器是否以及在何处记录了更多信息。我添加了
new.seek(0)
,如上面的代码所示,我仍然收到此错误。看起来这不会导致这里出现500错误,这只是一般性建议。嘿,我删除了print语句后面的变量以及print语句中与这些变量对应的符号,我的程序现在似乎可以运行了。你知道我在这些变量上哪里出错了吗?嘿,martijn。最后一个问题。似乎文件中已经存在的内容正在被
form.getvalue(“surveyQuestion”)
覆盖。你知道这是为什么吗?我只想将
form.getvalue(“surveyQuestion”)
附加到file@user3251142:
“r+”
将文件指针放在文件的开头<代码>'a+'应该可以工作(追加和读取),或者使用
.seek(0,2)
先搜索到底。你是救命恩人,我的代码现在很好地结合在一起:)只是为了理解,所以如果我使用r+并在我的文件中有>5个数字的地方写入5个数字,会发生什么?r+是否总是覆盖文件?编辑:刚刚测试了它和na,他们被推到了下一行。完全理解。@user3251142:您正在用5位数字和换行符覆盖文件中的前6个字符。这6个位置中前面的字符丢失。