1

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

1 Answer 1

1

I figured it out.You have to make your script executable and then restart the server.

chmod 705 .../folders/scriptname.cgi

will make the script executable.

Sign up to request clarification or add additional context in comments.

1 Comment

Restarting the server is not related at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.