Can not get params from URL - why is my cgi.FieldStorage() empty?

1.2k Views Asked by At

I cant get parameters from URL. After reading numerous posts on the web but nothing seems to answer my question...

I have a simple HTML file:

<html>
<head>
</head> 
<body>
    <form action="test.py" method="get">            
        Name: <input id="person_name" type="text" name="person_name" >          
        <input id="submit_button" type="submit" value="Submit" >        
    </form>     
</body>

and my python script looks like this:

import cgi, cgitb   

form = cgi.FieldStorage() 
name = str( form.getvalue('person_name') )

print ("Content-type:text/html\n\n")
print ("<html>")
print ("<head>")    
print ("</head>")
print ("<body>")
print ("Hello " + name )
print("<br/>")
print ("all params: " + str(form)  )
print ("</body>")
print ("</html>")

However, when I execute the HTML file and type "Mike" as a name, my python script prints:

Hello None
all params: FieldStorage(None, None, []) 

the cgi.FieldStorage() is empty. How can I fix it?

3

There are 3 best solutions below

1
On

It's hard to say.

<form action="test.py" method="get"> Error?

It should be <form action="cgi-bin/test.py" method="get">

If "cgi-bin" is not a part of the url sgi scripts are not executed. It's even surprising that you get some kind of response.

1
On

I get "500 Internal server error"

It's better now. It looks like it is starting working. The problem is that your python script has no declaration. It starts with import cgi, cgitb when it should start with #!/usr/bin/python or #!/usr/bin/env python. Apache doesn't know what kind of script it is.

0
On

the 500 internal server error is a problem with your apache script configuration file especially the pythonhandler and addhandlers area something to do with mod-python