views:

26

answers:

1

I have to create a HTML form and get the data using python-cgi. HTML form requires user to submit firstname and lastname and then the python script is supposed to get the data generated in the form. I have read up tutorials and tried playing with it to make it work, but it does not seem to happen. HTML code:

<form method = "POST" action ="http://localhost/cgi-bin/pyfile_test.py"&gt;
    <p>First Name: <input type="text" name="firstname">
    <p>Last Name: <input type="text" name="lastname">
    <p>Click here to submit the form:<input type="submit" value="Submit">
    <input type="hidden" name="session" value="1898fd">
    </form>

python script to extract data

#!c:/Python26/python.exe -u

import cgi, cgitb
cgitb.enable()

def main()
    print "Content-type: text/html\n"
    print
    form = cgi.FieldStorage()
    if form.has_key("firstname") amd form["firstname"].value !="":
    print "<h1>Hello", form["firstname"].value, "</h1>"
    else:
    print "<h1> Error!Wrong!</h1>
main()

when i direct html form to above script it does NOT work but when i direct it to the following script it works.

#!c:/Python26/python.exe -u

import cgi, cgitb
cgitb.enable()

print "Content-type: text/html"
print

print "The First Python Script On Server Side"
print "The sum of two numbers evaluation"
print "3+5 = ",3+5

I dont know what is going wrong. Any help will be highly appreciated!

A: 

At least in what you just showed, there are syntax errors:

if form.has_key("firstname") amd form["firstname"].value !="":
print "<h1>Hello", form["firstname"].value, "</h1>"
else:
print "<h1> Error!Wrong!</h1>

amd should be and, and both print statements should be indented deeper than the if and else keywords that control them.

If you fix your syntax and try again, what exactly do you see in the browser (and what do you see if you use your browser's "show source" functionality)?

Edit:

One more syntax error: you're missing a closing " on the second print. It's really tiresome to spot all your syntax errors one by one, and Apache (or whatever your web server is) doesn't seem to be helping -- cgitb can't take control until the whole script is successfully parsed, so syntax errors are the one reason it can't show!

And one more: missing colon at the end of the def main line.

Why don't you just run it with python yourscript.py at the command line until you've made syntax-error-free, and only then try again to run it as a CGI?

E.g. after fixing the two errors I spotted earlier but not the two I've just spotted, you'd be seeing (if python is on the PATH and the shell prompt is $):

$ python acgi.py 
  File "acgi.py", line 6
    def main()
             ^
SyntaxError: invalid syntax

because of the missing " at the end of the previous line and missing colon at line 6.

Fixing the last two errors (total of four in the code you originally posted) you finally see:

$ python acgi.py 
Content-type: text/html


<h1> Error!Wrong!</h1>

of course it says "wrong" because the field storage is empty, but this shows you can NOW (with all four errors fixed) sensibly try to run it as a CGI script at last;-)

Alex Martelli
I am getting the following error message (after correcting the syntax): Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log.
shubster
@shubster, two more syntax errors (a total of four in the code you originally posted), see my answer's Edit -- fixing all four of your syntax errors should finally let it run. Also in my A's Edit you'll see explanation of why I knew they had to be _syntax_ errors (otherwise cgitb, being enabled, would have been able to show you a traceback) and how to diagnose syntax error in your CGI script by running it as a **normal** script at the command line (since your server is not helpful in showing error tracebacks;-).
Alex Martelli