I am trying to create a form and getting the data entered by user into my script using python-cgi but I am getting this error message "End of script output before headers: processname.cgi". I have followed various blogs and tutorials but couldn't make it work.
Here is my form and cgi script. I am using python3 and xampp server on my local machine(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>
And this is my cgi-script: 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()