0
我想创建一个表单并将用户输入的数据导入到我的脚本中使用python-cgi但我收到此错误消息“End在header:processname.cgi之前的脚本输出“。我跟着各种博客和教程,但无法使其工作。错误:在python-cgi中提交html表单之前的标题输出结束
这是我的表格和CGI脚本。我使用python3和XAMPP服务器我的本地机器(MAC OSX)
form.html
<DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Get name from User</title>
</head>
<body>
<form method="post" action="processname.cgi">
Please Enter Your name: <input type="text" name="username" placeholder="e.g. John" autofocus
required="required"/>
<br>
<input type="submit" name="submitname" value="Submit"/>
</form>
</body>
</html>
这是我的CGI脚本:processname.cgi
#!/usr/local/bin/python3
import cgi
import cgitb
cgitb.enable()
def htmlTop():
print('''Content-type:text/html\n\n
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Server Side template</title>
</head>
<body>''')
def htmlTail():
print('''</body>
</html>''')
def getData():
formData = cgi.FieldStorage()
firstname = formData.getvalue("username")
return firstname
if __name__ == '__main__':
try:
htmlTop()
firstname = getData()
print("Hello {0}".format(firstname))
htmlTail()
except:
cgi.print_exception()