0% found this document useful (0 votes)
98 views

Python Cgi Samples

The documents show the evolution of a Python CGI program that takes user input of a first and last name from a web form, stores it in a MySQL database, and displays it back. Document 1 contains the initial sample Python CGI program that displays a basic HTML form. Document 2 adds functionality to retrieve and display the submitted form values. Document 3 modifies it to insert the values into a MySQL database. Document 4 removes the database table creation code. Document 5 changes it to a POST form and repopulates the submitted values.

Uploaded by

rhitika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Python Cgi Samples

The documents show the evolution of a Python CGI program that takes user input of a first and last name from a web form, stores it in a MySQL database, and displays it back. Document 1 contains the initial sample Python CGI program that displays a basic HTML form. Document 2 adds functionality to retrieve and display the submitted form values. Document 3 modifies it to insert the values into a MySQL database. Document 4 removes the database table creation code. Document 5 changes it to a POST form and repopulates the submitted values.

Uploaded by

rhitika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) Sample.

py

#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '<form action="/cgi-bin/hello_get.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>'

2) hello_get.py

#!/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"
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>"

3) hello_get.py(Modified 1)

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
import MySQLdb
# 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"
db = MySQLdb.connect("localhost","root","root","test" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
# Create table as per requirement
sql = """CREATE TABLE student (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20) )"""
cursor.execute(sql)
sql = "INSERT INTO student(FIRST_NAME, LAST_NAME) \
VALUES ('%s', '%s')" % (first_name, last_name)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
print "success
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()

4) hello_get.py(Modified 2)

#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
import MySQLdb
# 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"
db = MySQLdb.connect("localhost","root","root","test" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
# Create table as per requirement
'''sql = """CREATE TABLE student (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20) )"""
cursor.execute(sql)'''
sql = "INSERT INTO student(FIRST_NAME, LAST_NAME) \
VALUES ('%s', '%s')" % (first_name, last_name)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
print "<html>"
print "<head>"
print "<title>hello_get1.pyS</title>"
print "</head>"
print "<body>"
print "<h2>Success</h2>"
print "</body>"
print "</html>"
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()

5) hello_get.py(Modified 3)

#! /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"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print '<form action="hello_get.py" method="post">'
print 'First Name: <input type="text" name="first_name" value="%s"><br />' %
(first_name)
print 'Last Name: <input type="text" name="last_name" value="%s" />' % (last_name)
print '</form>'
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

You might also like