Python CGI: Difference between revisions

Line 43:
http://10.140.196.7/cgi-bin/cgi.py
 
= Get Inputsinputs fromform FormURL =
 
Create a test CGI script in cgi-bin dir:
nano indextest.py
 
nano index.py
<syntaxhighlight lang="python">
#!/usr/bin/python
 
# Import modules for CGI handling
import cgi, cgitb
 
# 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"
Line 65 ⟶ 67:
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print'<form action = "/cgi-bin/test.py" method = "get">'
print'First Name: <input type = "text" name = "first_name"> <br />'
 
print'Last Name: <input type = "text" name = "last_name" />'
print'<input type = "submit" value = "Submit" />'
print"</form>"
print "</body>"
print "</html>"
</syntaxhighlight>
 
Make it executable:
test@myserver:~/scripts/techsupp_analyzer$ cat test.py
sudo chmod +x test.py
 
Testing:
http://10.140.196.7/cgi-bin/test.py?first_name=My&last_name=Admin
 
#= Get dataInputs from fieldsForm =
 
File Structure:
--|---index.py
|
|---test.py
 
In the same dir as above test.py, create Form Page:
nano index.py
 
<syntaxhighlight lang="python">
#!/usr/bin/python
Line 81 ⟶ 93:
# Import modules for CGI handling
import cgi, cgitb
 
# 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"
Line 95 ⟶ 100:
print "</head>"
print "<body>"
print'<form action = "/cgi-bin/test.py" method = "get">'
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print'First Name: <input type = "text" name = "first_name"> <br />'
print'Last Name: <input type = "text" name = "last_name" />'
print'<input type = "submit" value = "Submit" />'
print"</form>"
print "</body>"
print "</html>"
</syntaxhighlight>
 
Make it executable:
sudo chmod +x index.py
 
Testing
http://10.140.196.7/cgi-bin/index.py