Running a Python cgi program 

Simple example without html
 The html to prompt for your name looks like this
 
<!DOCTYPE html>
<html>
<body>
<FORM ACTION="/cgi-bin/hello-python1.cgi" METHOD="POST">
<label><b>Please enter your name</b></label>
<INPUT TYPE="text" NAME="person" SIZE=30 >
<input type="submit" value="Submit" />
</form>
</body>
</html>

Note: you can also use javascript to prompt for the name rather than a form
See Javascript example1

The Python cgi code on the server looks like this
Simple example without html
#!/usr/bin/python
# Import modules for CGI handling 
import cgi, cgitb 
# Create instance of FieldStorage 
form = cgi.FieldStorage() 
# Get data from fields
person = form.getvalue('person')
print "Content-type:text/html\r\n\r\n"
print "hello %s" % (person)

Return web page (html) with results
#!/usr/bin/python
import cgi, cgitb 
form = cgi.FieldStorage() 
person = form.getvalue('person')

# Send a new html page so you can run it again
print "Content-type:text/html\r\n\r\n"
print """
<html>
<head>
<title>Hello - CGI Program</title>
</head>
<body>
<h2>Hello  %s </h2> 
<P>
Try it again<BR>
<form action="/cgi-bin/hello-python1.cgi" method="post">
<label><b>Please enter your name</b></label>
<input type="text" name="person" SIZE=30>
<input type="submit" value="Submit" />
</form>
</body>
</html>""" % (person)

last updated 4 Apr 2016