Python Cgi Samples
Python Cgi Samples
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
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>"