SlideShare a Scribd company logo
1 of 11
Download to read offline
http://www.tutorialspoint.com/python/python_cg i_prog ramming .htm Copyright © tutorialspoint.com
PYTHON CGI PROGRAMMING
What is CGI?
The CommonGateway Interface, or CGI, is a set of standards that define how informationis exchanged
betweenthe web server and a customscript.
The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows:
The CommonGateway Interface, or CGI, is a standard for externalgateway programs to interface with
informationservers suchas HTTP servers.
The current versionis CGI/1.1 and CGI/1.2 is under progress.
Web Browsing
To understand the concept of CGI, lets see what happens whenwe click a hyper link to browse a particular web
page or URL.
Your browser contacts the HTTP web server and demands for the URL i.e., filename.
Web Server willparse the URL and willlook for the filename inif it finds that file thensends it back to the
browser, otherwise sends anerror message indicating that youhave requested a wrong file.
Web browser takes response fromweb server and displays either the received file or error message.
However, it is possible to set up the HTTP server so that whenever a file ina certaindirectory is requested that
file is not sent back; instead it is executed as a program, and whatever that programoutputs is sent back for your
browser to display. This functionis called the CommonGateway Interface or CGI and the programs are called
CGI scripts. These CGI programs canbe a PythonScript, PERL Script, ShellScript, C or C++ program, etc.
CGI Architecture Diagram
Web Server Support & Configuration
Before youproceed withCGI Programming, make sure that your Web Server supports CGI and it is configured
to handle CGI Programs. Allthe CGI Programs to be executed by the HTTP server are kept ina pre-configured
directory. This directory is called CGI Directory and by conventionit is named as /var/www/cgi-bin. By
convention, CGI files willhave extensionas .cgi, but youcankeep your files withpythonextension.py as well.
By default, the Linux server is configured to runonly the scripts inthe cgi-bindirectory in/var/www. If youwant
to specify any other directory to runyour CGI scripts, comment the following lines inthe httpd.conf file:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/cgi-bin">
Options All
</Directory>
Here, I assumed that youhave Web Server up and running successfully and youare able to runany other CGI
programlike Perlor Shell, etc.
First CGI Program
Here is a simple link, whichis linked to a CGI script called hello.py. This file is being kept in/var/www/cgi-bin
directory and it has following content. Before running your CGI program, make sure youhave change mode of
file using chmod 755 hello.py UNIX command to make file executable.
#!/usr/bin/python
print "Content-type:text/htmlrnrn"
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 '</body>'
print '</html>'
If youclick hello.py, thenthis produces the following output:
Hello Word! This is my first CGI program
This hello.py script is a simple Pythonscript, whichis writing its output onSTDOUT file i.e., screen. There is one
important and extra feature available whichis first line to be printed Content-type:text/htmlrnrn. This
line is sent back to the browser and specifiy the content type to be displayed onthe browser screen.
Now, youmust have understood basic concept of CGI and youcanwrite many complicated CGI programs using
Python. This script caninteract withany other externalsystemalso to exchange informationsuchas RDBMS.
HTTP Header
The line Content-type:text/htmlrnrn is part of HTTP header whichis sent to the browser to
understand the content. Allthe HTTP header willbe inthe following form:
HTTP Field Name: Field Content
For Example
Content-type: text/htmlrnrn
There are few other important HTTP headers, whichyouwilluse frequently inyour CGI Programming.
Header Description
Content-type: A MIME string defining the format of the file being returned. Example is
Content-type:text/html
Expires: Date The date the informationbecomes invalid. This should be used by the
browser to decide whena page needs to be refreshed. A valid date string
should be inthe format 01 Jan1998 12:00:00 GMT.
Location: URL The URL that should be returned instead of the URL requested. Youcan
use this field to redirect a request to any file.
Last-modified: Date The date of last modificationof the resource.
Content-length: N The length, inbytes, of the data being returned. The browser uses this
value to report the estimated download time for a file.
Set-Cookie: String Set the cookie passed throughthe string
CGI Environment Variables
Allthe CGI programwillhave access to the following environment variables. These variables play animportant
role while writing any CGI program.
Variable Name Description
CONTENT_TYPE The data type of the content. Used whenthe client is sending attached
content to the server. For example, file upload, etc.
CONTENT_LENGTH The lengthof the query information. It's available only for POST requests.
HTTP_COOKIE Returns the set cookies inthe formof key & value pair.
HTTP_USER_AGENT The User-Agent request-header field contains informationabout the user
agent originating the request. Its name of the web browser.
PATH_INFO The pathfor the CGI script.
QUERY_STRING The URL-encoded informationthat is sent withGET method request.
REMOTE_ADDR The IP address of the remote host making the request. This canbe useful
for logging or for authenticationpurpose.
REMOTE_HOST The fully qualified name of the host making the request. If this informationis
not available thenREMOTE_ADDR canbe used to get IR address.
REQUEST_METHOD The method used to make the request. The most commonmethods are
GET and POST.
SCRIPT_FILENAME The fullpathto the CGI script.
SCRIPT_NAME The name of the CGI script.
SERVER_NAME The server's hostname or IP Address
SERVER_SOFTWARE The name and versionof the software the server is running.
Here is smallCGI programto list out allthe CGI variables. Click this link to see the result Get Environment
#!/usr/bin/python
import os
print "Content-type: text/htmlrnrn";
print "<font size=+1>Environment</font><br>";
for param in os.environ.keys():
print "<b>%20s</b>: %s<br>" % (param, os.environ[param])
GET and POST Methods
Youmust have come across many situations whenyouneed to pass some informationfromyour browser to web
server and ultimately to your CGI Program. Most frequently, browser uses two methods two pass this
informationto web server. These methods are GET Method and POST Method.
Passing Information using GET method:
The GET method sends the encoded user informationappended to the page request. The page and the
encoded informationare separated by the ? character as follows:
http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2
The GET method is the default method to pass informationfrombrowser to web server and it produces a long
string that appears inyour browser's Location:box. Never use GET method if youhave password or other
sensitive informationto pass to the server. The GET method has size limtation: only 1024 characters canbe sent
ina request string. The GET method sends informationusing QUERY_STRING header and willbe accessible in
your CGI ProgramthroughQUERY_STRING environment variable.
Youcanpass informationby simply concatenating key and value pairs along withany URL or youcanuse HTML
<FORM> tags to pass informationusing GET method.
Simple URL Example : Get Method
Here is a simple URL, whichwillpass two values to hello_get.py programusing GET method.
/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
Below is hello_get.py script to handle input givenby web browser. We are going to use cgi module, which
makes it very easy to access passed information:
#!/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/htmlrnrn"
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>"
This would generate the following result:
Hello ZARA ALI
Simple FORM Example: GET Method
Here is a simple example whichpasses two values using HTML FORM and submit button. We are going to use
same CGI script hello_get.py to handle this imput.
<form action="/cgi-bin/hello_get.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>
Here is the actualoutput of the above form, Youenter First and Last Name and thenclick submit buttonto see the
result.
First Name:
Last Name:
Passing Information using POST method:
A generally more reliable method of passing informationto a CGI programis the POST method. This packages
the informationinexactly the same way as GET methods, but instead of sending it as a text string after a ? inthe
URL it sends it as a separate message. This message comes into the CGI script inthe formof the standard input.
Below is same hello_get.py script whichhandles GET as wellas POST method.
#!/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/htmlrnrn"
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>"
Let us take againsame example as above whichpasses two values using HTML FORM and submit button. We
are going to use same CGI script hello_get.py to handle this imput.
<form action="/cgi-bin/hello_get.py" method="post">
First Name: <input type="text" name="first_name"><br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
Here is the actualoutput of the above form. Youenter First and Last Name and thenclick submit buttonto see the
result.
First Name:
Last Name:
Passing Checkbox Data to CGI Program
Checkboxes are used whenmore thanone optionis required to be selected.
Here is example HTML code for a formwithtwo checkboxes:
<form action="/cgi-bin/checkbox.cgi" method="POST" target="_blank">
<input type="checkbox" name="maths" value="on" /> Maths
<input type="checkbox" name="physics" value="on" /> Physics
<input type="submit" value="Select Subject" />
</form>
The result of this code is the following form:
Maths Physics
Below is checkbox.cgiscript to handle input givenby web browser for checkbox button.
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('maths'):
math_flag = "ON"
else:
math_flag = "OFF"
if form.getvalue('physics'):
physics_flag = "ON"
else:
physics_flag = "OFF"
print "Content-type:text/htmlrnrn"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"
Passing Radio Button Data to CGI Program
Radio Buttons are used whenonly one optionis required to be selected.
Here is example HTML code for a formwithtwo radio buttons:
<form action="/cgi-bin/radiobutton.py" method="post" target="_blank">
<input type="radio" name="subject" value="maths" /> Maths
<input type="radio" name="subject" value="physics" /> Physics
<input type="submit" value="Select Subject" />
</form>
The result of this code is the following form:
Maths Physics
Below is radiobutton.py script to handle input givenby web browser for radio button:
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('subject'):
subject = form.getvalue('subject')
else:
subject = "Not set"
print "Content-type:text/htmlrnrn"
print "<html>"
print "<head>"
print "<title>Radio - Fourth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
Passing Text Area Data to CGI Program
TEXTAREA element is used whenmultiline text has to be passed to the CGI Program.
Here is example HTML code for a formwitha TEXTAREA box:
<form action="/cgi-bin/textarea.py" method="post" target="_blank">
<textarea name="textcontent" cols="40" rows="4">
Type your text here...
</textarea>
<input type="submit" value="Submit" />
</form>
The result of this code is the following form:
Type your text here...
Below is textarea.cgiscript to handle input givenby web browser:
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('textcontent'):
text_content = form.getvalue('textcontent')
else:
text_content = "Not entered"
print "Content-type:text/htmlrnrn"
print "<html>"
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Entered Text Content is %s</h2>" % text_content
print "</body>"
Passing Drop Down Box Data to CGI Program
Drop DownBox is used whenwe have many options available but only one or two willbe selected.
Here is example HTML code for a formwithone drop downbox:
<form action="/cgi-bin/dropdown.py" method="post" target="_blank">
<select name="dropdown">
<option value="Maths" selected>Maths</option>
<option value="Physics">Physics</option>
</select>
<input type="submit" value="Submit"/>
</form>
The result of this code is the following form:
Maths
Below is dropdown.py script to handle input givenby web browser.
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
if form.getvalue('dropdown'):
subject = form.getvalue('dropdown')
else:
subject = "Not entered"
print "Content-type:text/htmlrnrn"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
Using Cookies in CGI
HTTP protocolis a stateless protocol. But for a commercialwebsite, it is required to maintainsession
informationamong different pages. For example, one user registrationends after completing many pages. But
how to maintainuser's sessioninformationacross allthe web pages.
Inmany situations, using cookies is the most efficient method of remembering and tracking preferences,
purchases, commissions, and other informationrequired for better visitor experience or site statistics.
How It Works?
Your server sends some data to the visitor's browser inthe formof a cookie. The browser may accept the
cookie. If it does, it is stored as a plaintext record onthe visitor's hard drive. Now, whenthe visitor arrives at
another page onyour site, the cookie is available for retrieval. Once retrieved, your server knows/remembers
what was stored.
Cookies are a plaintext data record of 5 variable-lengthfields:
Expires : The date the cookie willexpire. If this is blank, the cookie willexpire whenthe visitor quits the
browser.
Domain : The domainname of your site.
Path : The pathto the directory or web page that sets the cookie. This may be blank if youwant to
retrieve the cookie fromany directory or page.
Secure : If this field contains the word "secure", thenthe cookie may only be retrieved witha secure
server. If this field is blank, no suchrestrictionexists.
Name=Value : Cookies are set and retrieved inthe formof key and value pairs.
Setting up Cookies
It is very easy to send cookies to browser. These cookies willbe sent along withHTTP Header before to
Content-type field. Assuming youwant to set UserID and Password as cookies. So cookies setting willbe done
as follows:
#!/usr/bin/python
print "Set-Cookie:UserID=XYZ;rn"
print "Set-Cookie:Password=XYZ123;rn"
print "Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT";rn"
print "Set-Cookie:Domain=www.tutorialspoint.com;rn"
print "Set-Cookie:Path=/perl;n"
print "Content-type:text/htmlrnrn"
...........Rest of the HTML Content....
Fromthis example, youmust have understood how to set cookies. We use Set-Cookie HTTP header to set
cookies.
Here, it is optionalto set cookies attributes like Expires, Domainand Path. It is notable that cookies are set
before sending magic line "Content-type:text/htmlrnrn.
Retrieving Cookies
It is very easy to retrieve allthe set cookies. Cookies are stored inCGI environment variable HTTP_COOKIE
and they willhave following form:
key1=value1;key2=value2;key3=value3....
Here is anexample of how to retrieve cookies.
#!/usr/bin/python
# Import modules for CGI handling
from os import environ
import cgi, cgitb
if environ.has_key('HTTP_COOKIE'):
for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
(key, value ) = split(cookie, '=');
if key == "UserID":
user_id = value
if key == "Password":
password = value
print "User ID = %s" % user_id
print "Password = %s" % password
This willproduce the following result for the cookies set by above script:
User ID = XYZ
Password = XYZ123
File Upload Example:
To upload a file, the HTML formmust have the enctype attribute set to multipart/form-data. The input tag
withthe file type willcreate a "Browse" button.
<html>
<body>
<form enctype="multipart/form-data"
action="save_file.py" method="post">
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>
The result of this code is the following form:
File:
Above example has beendisabled intentionally to save people uploading file onour server, but youcantry above
code withyour server.
Here is the script save_file.py to handle file upload:
#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """
Content-Type: text/htmln
<html>
<body>
<p>%s</p>
</body>
</html>
""" % (message,)
If youare running above script onUnix/Linux, thenyouwould have to take care of replacing file separator as
follows, otherwise onyour windows machine above open() statement should work fine.
fn = os.path.basename(fileitem.filename.replace("", "/" ))
How To Raise a "File Download" Dialog Box ?
Sometimes, it is desired that youwant to give optionwhere a user willclick a link and it willpop up a "File
Download" dialogue box to the user instead of displaying actualcontent. This is very easy and willbe achieved
throughHTTP header. This HTTP header willbe different fromthe header mentioned inprevious section.
For example,if youwant make a FileName file downloadable froma givenlink, thenits syntax willbe as follows:
#!/usr/bin/python
# HTTP Header
print "Content-Type:application/octet-stream; name="FileName"rn";
print "Content-Disposition: attachment; filename="FileName"rnn";
# Actual File Content will go hear.
fo = open("foo.txt", "rb")
str = fo.read();
print str
# Close opend file
fo.close()
Hope youenjoyed this tutorial. If yes, please send me your feedback at: Contact Us

More Related Content

What's hot

Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
An introduction to Jupyter notebooks and the Noteable service
An introduction to Jupyter notebooks and the Noteable serviceAn introduction to Jupyter notebooks and the Noteable service
An introduction to Jupyter notebooks and the Noteable serviceJisc
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Edureka!
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaEdureka!
 
Socket programming
Socket programmingSocket programming
Socket programmingNemiRathore
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit Satam
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaEdureka!
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and DjangoMichael Pirnat
 

What's hot (20)

File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
Python libraries
Python librariesPython libraries
Python libraries
 
An introduction to Jupyter notebooks and the Noteable service
An introduction to Jupyter notebooks and the Noteable serviceAn introduction to Jupyter notebooks and the Noteable service
An introduction to Jupyter notebooks and the Noteable service
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | Edureka
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and Django
 

Similar to Python cgi programming (20)

Copy of cgi
Copy of cgiCopy of cgi
Copy of cgi
 
Cgi
CgiCgi
Cgi
 
CGI by rj
CGI by rjCGI by rj
CGI by rj
 
CGI Presentation
CGI PresentationCGI Presentation
CGI Presentation
 
Introduction to Web Programming with Perl
Introduction to Web Programming with PerlIntroduction to Web Programming with Perl
Introduction to Web Programming with Perl
 
Fm 2
Fm 2Fm 2
Fm 2
 
How cgi scripting works
How cgi scripting worksHow cgi scripting works
How cgi scripting works
 
Slides serverside main
Slides serverside mainSlides serverside main
Slides serverside main
 
Apache Web Server Setup 3
Apache Web Server Setup 3Apache Web Server Setup 3
Apache Web Server Setup 3
 
are available here
are available hereare available here
are available here
 
Common Gateway Interface
Common Gateway InterfaceCommon Gateway Interface
Common Gateway Interface
 
Apache2 BootCamp : Serving Dynamic Content with CGI
Apache2 BootCamp : Serving Dynamic Content with CGIApache2 BootCamp : Serving Dynamic Content with CGI
Apache2 BootCamp : Serving Dynamic Content with CGI
 
PPT
PPTPPT
PPT
 
5-WebServers.ppt
5-WebServers.ppt5-WebServers.ppt
5-WebServers.ppt
 
Cache control directive
Cache control directiveCache control directive
Cache control directive
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Cgi
CgiCgi
Cgi
 
Perl web programming
Perl web programmingPerl web programming
Perl web programming
 
Unit 02: Web Technologies (2/2)
Unit 02: Web Technologies (2/2)Unit 02: Web Technologies (2/2)
Unit 02: Web Technologies (2/2)
 
Cgi
CgiCgi
Cgi
 

More from Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai

More from Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai (20)

Artificial Intelligence (AI) application in Agriculture Area
Artificial Intelligence (AI) application in Agriculture Area Artificial Intelligence (AI) application in Agriculture Area
Artificial Intelligence (AI) application in Agriculture Area
 
VLSI Design Book CMOS_Circuit_Design__Layout__and_Simulation
VLSI Design Book CMOS_Circuit_Design__Layout__and_SimulationVLSI Design Book CMOS_Circuit_Design__Layout__and_Simulation
VLSI Design Book CMOS_Circuit_Design__Layout__and_Simulation
 
Question Bank: Network Management in Telecommunication
Question Bank: Network Management in TelecommunicationQuestion Bank: Network Management in Telecommunication
Question Bank: Network Management in Telecommunication
 
INTRODUCTION TO CYBER LAW The Concept of Cyberspace Cyber law Cyber crime.pdf
INTRODUCTION TO CYBER LAW The Concept of Cyberspace Cyber law Cyber crime.pdfINTRODUCTION TO CYBER LAW The Concept of Cyberspace Cyber law Cyber crime.pdf
INTRODUCTION TO CYBER LAW The Concept of Cyberspace Cyber law Cyber crime.pdf
 
LRU_Replacement-Policy.pdf
LRU_Replacement-Policy.pdfLRU_Replacement-Policy.pdf
LRU_Replacement-Policy.pdf
 
Network Management Principles and Practice - 2nd Edition (2010)_2.pdf
Network Management Principles and Practice - 2nd Edition (2010)_2.pdfNetwork Management Principles and Practice - 2nd Edition (2010)_2.pdf
Network Management Principles and Practice - 2nd Edition (2010)_2.pdf
 
Euler Method Details
Euler Method Details Euler Method Details
Euler Method Details
 
Mini Project fo BE Engineering students
Mini Project fo BE Engineering  students  Mini Project fo BE Engineering  students
Mini Project fo BE Engineering students
 
Mini Project for Engineering Students BE or Btech Engineering students
Mini Project for Engineering Students BE or Btech Engineering students Mini Project for Engineering Students BE or Btech Engineering students
Mini Project for Engineering Students BE or Btech Engineering students
 
Ballistics Detsils
Ballistics Detsils Ballistics Detsils
Ballistics Detsils
 
VLSI Design_LAB MANUAL By Umakant Gohatre
VLSI Design_LAB MANUAL By Umakant GohatreVLSI Design_LAB MANUAL By Umakant Gohatre
VLSI Design_LAB MANUAL By Umakant Gohatre
 
Cryptography and Network Security
Cryptography and Network SecurityCryptography and Network Security
Cryptography and Network Security
 
cyber crime, Cyber Security, Introduction, Umakant Bhaskar Gohatre
cyber crime, Cyber Security, Introduction, Umakant Bhaskar Gohatre cyber crime, Cyber Security, Introduction, Umakant Bhaskar Gohatre
cyber crime, Cyber Security, Introduction, Umakant Bhaskar Gohatre
 
Image Compression, Introduction Data Compression/ Data compression, modelling...
Image Compression, Introduction Data Compression/ Data compression, modelling...Image Compression, Introduction Data Compression/ Data compression, modelling...
Image Compression, Introduction Data Compression/ Data compression, modelling...
 
Introduction Data Compression/ Data compression, modelling and coding,Image C...
Introduction Data Compression/ Data compression, modelling and coding,Image C...Introduction Data Compression/ Data compression, modelling and coding,Image C...
Introduction Data Compression/ Data compression, modelling and coding,Image C...
 
Python overview
Python overviewPython overview
Python overview
 
Python numbers
Python numbersPython numbers
Python numbers
 
Python networking
Python networkingPython networking
Python networking
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Python modules
Python modulesPython modules
Python modules
 

Recently uploaded

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

Python cgi programming

  • 1. http://www.tutorialspoint.com/python/python_cg i_prog ramming .htm Copyright © tutorialspoint.com PYTHON CGI PROGRAMMING What is CGI? The CommonGateway Interface, or CGI, is a set of standards that define how informationis exchanged betweenthe web server and a customscript. The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows: The CommonGateway Interface, or CGI, is a standard for externalgateway programs to interface with informationservers suchas HTTP servers. The current versionis CGI/1.1 and CGI/1.2 is under progress. Web Browsing To understand the concept of CGI, lets see what happens whenwe click a hyper link to browse a particular web page or URL. Your browser contacts the HTTP web server and demands for the URL i.e., filename. Web Server willparse the URL and willlook for the filename inif it finds that file thensends it back to the browser, otherwise sends anerror message indicating that youhave requested a wrong file. Web browser takes response fromweb server and displays either the received file or error message. However, it is possible to set up the HTTP server so that whenever a file ina certaindirectory is requested that file is not sent back; instead it is executed as a program, and whatever that programoutputs is sent back for your browser to display. This functionis called the CommonGateway Interface or CGI and the programs are called CGI scripts. These CGI programs canbe a PythonScript, PERL Script, ShellScript, C or C++ program, etc. CGI Architecture Diagram Web Server Support & Configuration Before youproceed withCGI Programming, make sure that your Web Server supports CGI and it is configured to handle CGI Programs. Allthe CGI Programs to be executed by the HTTP server are kept ina pre-configured directory. This directory is called CGI Directory and by conventionit is named as /var/www/cgi-bin. By convention, CGI files willhave extensionas .cgi, but youcankeep your files withpythonextension.py as well. By default, the Linux server is configured to runonly the scripts inthe cgi-bindirectory in/var/www. If youwant to specify any other directory to runyour CGI scripts, comment the following lines inthe httpd.conf file: <Directory "/var/www/cgi-bin"> AllowOverride None Options ExecCGI Order allow,deny
  • 2. Allow from all </Directory> <Directory "/var/www/cgi-bin"> Options All </Directory> Here, I assumed that youhave Web Server up and running successfully and youare able to runany other CGI programlike Perlor Shell, etc. First CGI Program Here is a simple link, whichis linked to a CGI script called hello.py. This file is being kept in/var/www/cgi-bin directory and it has following content. Before running your CGI program, make sure youhave change mode of file using chmod 755 hello.py UNIX command to make file executable. #!/usr/bin/python print "Content-type:text/htmlrnrn" 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 '</body>' print '</html>' If youclick hello.py, thenthis produces the following output: Hello Word! This is my first CGI program This hello.py script is a simple Pythonscript, whichis writing its output onSTDOUT file i.e., screen. There is one important and extra feature available whichis first line to be printed Content-type:text/htmlrnrn. This line is sent back to the browser and specifiy the content type to be displayed onthe browser screen. Now, youmust have understood basic concept of CGI and youcanwrite many complicated CGI programs using Python. This script caninteract withany other externalsystemalso to exchange informationsuchas RDBMS. HTTP Header The line Content-type:text/htmlrnrn is part of HTTP header whichis sent to the browser to understand the content. Allthe HTTP header willbe inthe following form: HTTP Field Name: Field Content For Example Content-type: text/htmlrnrn There are few other important HTTP headers, whichyouwilluse frequently inyour CGI Programming. Header Description Content-type: A MIME string defining the format of the file being returned. Example is Content-type:text/html Expires: Date The date the informationbecomes invalid. This should be used by the browser to decide whena page needs to be refreshed. A valid date string should be inthe format 01 Jan1998 12:00:00 GMT.
  • 3. Location: URL The URL that should be returned instead of the URL requested. Youcan use this field to redirect a request to any file. Last-modified: Date The date of last modificationof the resource. Content-length: N The length, inbytes, of the data being returned. The browser uses this value to report the estimated download time for a file. Set-Cookie: String Set the cookie passed throughthe string CGI Environment Variables Allthe CGI programwillhave access to the following environment variables. These variables play animportant role while writing any CGI program. Variable Name Description CONTENT_TYPE The data type of the content. Used whenthe client is sending attached content to the server. For example, file upload, etc. CONTENT_LENGTH The lengthof the query information. It's available only for POST requests. HTTP_COOKIE Returns the set cookies inthe formof key & value pair. HTTP_USER_AGENT The User-Agent request-header field contains informationabout the user agent originating the request. Its name of the web browser. PATH_INFO The pathfor the CGI script. QUERY_STRING The URL-encoded informationthat is sent withGET method request. REMOTE_ADDR The IP address of the remote host making the request. This canbe useful for logging or for authenticationpurpose. REMOTE_HOST The fully qualified name of the host making the request. If this informationis not available thenREMOTE_ADDR canbe used to get IR address. REQUEST_METHOD The method used to make the request. The most commonmethods are GET and POST. SCRIPT_FILENAME The fullpathto the CGI script. SCRIPT_NAME The name of the CGI script. SERVER_NAME The server's hostname or IP Address SERVER_SOFTWARE The name and versionof the software the server is running. Here is smallCGI programto list out allthe CGI variables. Click this link to see the result Get Environment #!/usr/bin/python import os print "Content-type: text/htmlrnrn"; print "<font size=+1>Environment</font><br>"; for param in os.environ.keys(): print "<b>%20s</b>: %s<br>" % (param, os.environ[param]) GET and POST Methods
  • 4. Youmust have come across many situations whenyouneed to pass some informationfromyour browser to web server and ultimately to your CGI Program. Most frequently, browser uses two methods two pass this informationto web server. These methods are GET Method and POST Method. Passing Information using GET method: The GET method sends the encoded user informationappended to the page request. The page and the encoded informationare separated by the ? character as follows: http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2 The GET method is the default method to pass informationfrombrowser to web server and it produces a long string that appears inyour browser's Location:box. Never use GET method if youhave password or other sensitive informationto pass to the server. The GET method has size limtation: only 1024 characters canbe sent ina request string. The GET method sends informationusing QUERY_STRING header and willbe accessible in your CGI ProgramthroughQUERY_STRING environment variable. Youcanpass informationby simply concatenating key and value pairs along withany URL or youcanuse HTML <FORM> tags to pass informationusing GET method. Simple URL Example : Get Method Here is a simple URL, whichwillpass two values to hello_get.py programusing GET method. /cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI Below is hello_get.py script to handle input givenby web browser. We are going to use cgi module, which makes it very easy to access passed information: #!/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/htmlrnrn" 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>" This would generate the following result: Hello ZARA ALI Simple FORM Example: GET Method Here is a simple example whichpasses two values using HTML FORM and submit button. We are going to use same CGI script hello_get.py to handle this imput. <form action="/cgi-bin/hello_get.py" method="get">
  • 5. First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> Here is the actualoutput of the above form, Youenter First and Last Name and thenclick submit buttonto see the result. First Name: Last Name: Passing Information using POST method: A generally more reliable method of passing informationto a CGI programis the POST method. This packages the informationinexactly the same way as GET methods, but instead of sending it as a text string after a ? inthe URL it sends it as a separate message. This message comes into the CGI script inthe formof the standard input. Below is same hello_get.py script whichhandles GET as wellas POST method. #!/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/htmlrnrn" 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>" Let us take againsame example as above whichpasses two values using HTML FORM and submit button. We are going to use same CGI script hello_get.py to handle this imput. <form action="/cgi-bin/hello_get.py" method="post"> First Name: <input type="text" name="first_name"><br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> Here is the actualoutput of the above form. Youenter First and Last Name and thenclick submit buttonto see the result. First Name: Last Name: Passing Checkbox Data to CGI Program Checkboxes are used whenmore thanone optionis required to be selected. Here is example HTML code for a formwithtwo checkboxes:
  • 6. <form action="/cgi-bin/checkbox.cgi" method="POST" target="_blank"> <input type="checkbox" name="maths" value="on" /> Maths <input type="checkbox" name="physics" value="on" /> Physics <input type="submit" value="Select Subject" /> </form> The result of this code is the following form: Maths Physics Below is checkbox.cgiscript to handle input givenby web browser for checkbox button. #!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('maths'): math_flag = "ON" else: math_flag = "OFF" if form.getvalue('physics'): physics_flag = "ON" else: physics_flag = "OFF" print "Content-type:text/htmlrnrn" print "<html>" print "<head>" print "<title>Checkbox - Third CGI Program</title>" print "</head>" print "<body>" print "<h2> CheckBox Maths is : %s</h2>" % math_flag print "<h2> CheckBox Physics is : %s</h2>" % physics_flag print "</body>" print "</html>" Passing Radio Button Data to CGI Program Radio Buttons are used whenonly one optionis required to be selected. Here is example HTML code for a formwithtwo radio buttons: <form action="/cgi-bin/radiobutton.py" method="post" target="_blank"> <input type="radio" name="subject" value="maths" /> Maths <input type="radio" name="subject" value="physics" /> Physics <input type="submit" value="Select Subject" /> </form> The result of this code is the following form: Maths Physics Below is radiobutton.py script to handle input givenby web browser for radio button: #!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage
  • 7. form = cgi.FieldStorage() # Get data from fields if form.getvalue('subject'): subject = form.getvalue('subject') else: subject = "Not set" print "Content-type:text/htmlrnrn" print "<html>" print "<head>" print "<title>Radio - Fourth CGI Program</title>" print "</head>" print "<body>" print "<h2> Selected Subject is %s</h2>" % subject print "</body>" print "</html>" Passing Text Area Data to CGI Program TEXTAREA element is used whenmultiline text has to be passed to the CGI Program. Here is example HTML code for a formwitha TEXTAREA box: <form action="/cgi-bin/textarea.py" method="post" target="_blank"> <textarea name="textcontent" cols="40" rows="4"> Type your text here... </textarea> <input type="submit" value="Submit" /> </form> The result of this code is the following form: Type your text here... Below is textarea.cgiscript to handle input givenby web browser: #!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('textcontent'): text_content = form.getvalue('textcontent') else: text_content = "Not entered" print "Content-type:text/htmlrnrn" print "<html>" print "<head>"; print "<title>Text Area - Fifth CGI Program</title>" print "</head>" print "<body>" print "<h2> Entered Text Content is %s</h2>" % text_content print "</body>"
  • 8. Passing Drop Down Box Data to CGI Program Drop DownBox is used whenwe have many options available but only one or two willbe selected. Here is example HTML code for a formwithone drop downbox: <form action="/cgi-bin/dropdown.py" method="post" target="_blank"> <select name="dropdown"> <option value="Maths" selected>Maths</option> <option value="Physics">Physics</option> </select> <input type="submit" value="Submit"/> </form> The result of this code is the following form: Maths Below is dropdown.py script to handle input givenby web browser. #!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('dropdown'): subject = form.getvalue('dropdown') else: subject = "Not entered" print "Content-type:text/htmlrnrn" print "<html>" print "<head>" print "<title>Dropdown Box - Sixth CGI Program</title>" print "</head>" print "<body>" print "<h2> Selected Subject is %s</h2>" % subject print "</body>" print "</html>" Using Cookies in CGI HTTP protocolis a stateless protocol. But for a commercialwebsite, it is required to maintainsession informationamong different pages. For example, one user registrationends after completing many pages. But how to maintainuser's sessioninformationacross allthe web pages. Inmany situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other informationrequired for better visitor experience or site statistics. How It Works? Your server sends some data to the visitor's browser inthe formof a cookie. The browser may accept the cookie. If it does, it is stored as a plaintext record onthe visitor's hard drive. Now, whenthe visitor arrives at another page onyour site, the cookie is available for retrieval. Once retrieved, your server knows/remembers what was stored. Cookies are a plaintext data record of 5 variable-lengthfields: Expires : The date the cookie willexpire. If this is blank, the cookie willexpire whenthe visitor quits the browser. Domain : The domainname of your site.
  • 9. Path : The pathto the directory or web page that sets the cookie. This may be blank if youwant to retrieve the cookie fromany directory or page. Secure : If this field contains the word "secure", thenthe cookie may only be retrieved witha secure server. If this field is blank, no suchrestrictionexists. Name=Value : Cookies are set and retrieved inthe formof key and value pairs. Setting up Cookies It is very easy to send cookies to browser. These cookies willbe sent along withHTTP Header before to Content-type field. Assuming youwant to set UserID and Password as cookies. So cookies setting willbe done as follows: #!/usr/bin/python print "Set-Cookie:UserID=XYZ;rn" print "Set-Cookie:Password=XYZ123;rn" print "Set-Cookie:Expires=Tuesday, 31-Dec-2007 23:12:40 GMT";rn" print "Set-Cookie:Domain=www.tutorialspoint.com;rn" print "Set-Cookie:Path=/perl;n" print "Content-type:text/htmlrnrn" ...........Rest of the HTML Content.... Fromthis example, youmust have understood how to set cookies. We use Set-Cookie HTTP header to set cookies. Here, it is optionalto set cookies attributes like Expires, Domainand Path. It is notable that cookies are set before sending magic line "Content-type:text/htmlrnrn. Retrieving Cookies It is very easy to retrieve allthe set cookies. Cookies are stored inCGI environment variable HTTP_COOKIE and they willhave following form: key1=value1;key2=value2;key3=value3.... Here is anexample of how to retrieve cookies. #!/usr/bin/python # Import modules for CGI handling from os import environ import cgi, cgitb if environ.has_key('HTTP_COOKIE'): for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')): (key, value ) = split(cookie, '='); if key == "UserID": user_id = value if key == "Password": password = value print "User ID = %s" % user_id print "Password = %s" % password This willproduce the following result for the cookies set by above script: User ID = XYZ Password = XYZ123 File Upload Example: To upload a file, the HTML formmust have the enctype attribute set to multipart/form-data. The input tag
  • 10. withthe file type willcreate a "Browse" button. <html> <body> <form enctype="multipart/form-data" action="save_file.py" method="post"> <p>File: <input type="file" name="filename" /></p> <p><input type="submit" value="Upload" /></p> </form> </body> </html> The result of this code is the following form: File: Above example has beendisabled intentionally to save people uploading file onour server, but youcantry above code withyour server. Here is the script save_file.py to handle file upload: #!/usr/bin/python import cgi, os import cgitb; cgitb.enable() form = cgi.FieldStorage() # Get filename here. fileitem = form['filename'] # Test if the file was uploaded if fileitem.filename: # strip leading path from file name to avoid # directory traversal attacks fn = os.path.basename(fileitem.filename) open('/tmp/' + fn, 'wb').write(fileitem.file.read()) message = 'The file "' + fn + '" was uploaded successfully' else: message = 'No file was uploaded' print """ Content-Type: text/htmln <html> <body> <p>%s</p> </body> </html> """ % (message,) If youare running above script onUnix/Linux, thenyouwould have to take care of replacing file separator as follows, otherwise onyour windows machine above open() statement should work fine. fn = os.path.basename(fileitem.filename.replace("", "/" )) How To Raise a "File Download" Dialog Box ? Sometimes, it is desired that youwant to give optionwhere a user willclick a link and it willpop up a "File Download" dialogue box to the user instead of displaying actualcontent. This is very easy and willbe achieved throughHTTP header. This HTTP header willbe different fromthe header mentioned inprevious section.
  • 11. For example,if youwant make a FileName file downloadable froma givenlink, thenits syntax willbe as follows: #!/usr/bin/python # HTTP Header print "Content-Type:application/octet-stream; name="FileName"rn"; print "Content-Disposition: attachment; filename="FileName"rnn"; # Actual File Content will go hear. fo = open("foo.txt", "rb") str = fo.read(); print str # Close opend file fo.close() Hope youenjoyed this tutorial. If yes, please send me your feedback at: Contact Us