How can I pass parameters to python?

972 views Asked by At

The python parameter is not passed.

I think cgi.FieldStorage () does not seem to work. What settings should I add?

cgi.html :

<form action="cgi.py" method="get">
First Name: <input type="text" name="first_name">  <br />

Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>

cgi.py :

# Import modules for CGI handling
import cgi, cgitb
cgitb.enable()

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

chrome result:

enter image description here apache version:

httpd-2.4.6-40.el7.centos.4.x86_64
python-2.7.5-48.el7.x86_64
1

There are 1 answers

0
Hemang Rindani On

Try using If else condition while you call form = cgi.FieldStorage()

Apart from that, code and formatting look good to me. However, you can go through a simple tutorial as below. This will help you to fill the values from a form and will look something like this value = form.getlist(" ")

Ref: https://docs.python.org/2/library/cgi.html