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()