0% found this document useful (0 votes)
19 views52 pages

6135f8f2e69911630927090m11 CGI Programming

Python CGI allows dynamic interaction between a web application and client. It uses CGI scripts to process requests and output responses. The cgi module handles form data as a FieldStorage object containing keys and values that can then be processed by the script.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views52 pages

6135f8f2e69911630927090m11 CGI Programming

Python CGI allows dynamic interaction between a web application and client. It uses CGI scripts to process requests and output responses. The cgi module handles form data as a FieldStorage object containing keys and values that can then be processed by the script.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

CGI Programming

© Jetking Infotrain Ltd. 2021


Pre-class activities

Yoga
Attendance marking
Recall
Presentation

© Jetking Infotrain Ltd. 2021


Questions for Recall (5 minutes)

• What is logging?
• Why we are using pprint?

© Jetking Infotrain Ltd. 2021


Module Overview
• Python CGI
• Architectural diagram
• Python cgi module
• Xampp
• First CGI Program
• Directory structure
• FieldStorage
• GET method
© Jetking Infotrain Ltd. 2021
Module Overview

• FORM Example
• HTML FORM : GET Method
• HTML FORM : POST Method

© Jetking Infotrain Ltd. 2021


Python CGI

• Python CGI stands for Common Gateway Interface.


• It is not a type of language but a set of rules that establishes a
dynamic interaction between a web application and the client
application.
• The programs based on CGI helps in communicating between
the web servers and the client.

© Jetking Infotrain Ltd. 2021


Python CGI

• Python CGI module handles situations and helps debug scripts.


• Python CGI support for the uploading files through a HTML
form.
• Whenever the client-server requests the webserver, the
Common Gateway Interface (CGI) handles these requests using
external script files.

© Jetking Infotrain Ltd. 2021


Architectural diagram
• An architectural diagram representing the working of
CGI is shown below:

© Jetking Infotrain Ltd. 2021


Python cgi module

• Let’s begin with the Python cgi module. First, let’s import it.

• Now, We will use cgitb.enable() in our script to stimulate an


exception handler in the web browser to display the detailed
report for the errors that occurred.

© Jetking Infotrain Ltd. 2021


Python cgi module

• Now, we can save the report with the help of the following
script.

© Jetking Infotrain Ltd. 2021


Python cgi module

• The function of the cgi module stated above would help


throughout the script development.
• These reports help the user for debugging the script efficiently.
• Whenever the users get the expected result, they can eliminate
this.

© Jetking Infotrain Ltd. 2021


XAMPP

• Before start with CGI programming with python, first make


sure that your web server supports CGI and it is configured to
the handle CGI programs.
• Web server can be WAMPP, XAMPP, etc.
• We need to start the Apache server then execute the CGI script.
• If you already have the XAMPP server in your system then
run/open it now

© Jetking Infotrain Ltd. 2021


XAMPP

© Jetking Infotrain Ltd. 2021


XAMPP

• XAMPP stands for cross-platform, Apache, MySQL, PHP,


and Perl, and it provides the localhost server to test or deploy
websites.
• Generally, it gives the two essential components for its
installation, first is - Apache that creates a local server and
MySQL, which we can use a database.

© Jetking Infotrain Ltd. 2021


First CGI Program

• We have created a new folder called python301 in xampp's


htdocs folder.

• Then, we write a Python script, which includes the HTML tags.

• Let's see the following directory structure and the


first_CGI_script.py file.

© Jetking Infotrain Ltd. 2021


Directory structure

© Jetking Infotrain Ltd. 2021


first_CGI_script.py

© Jetking Infotrain Ltd. 2021


first_CGI_script.py

• Now start the Apache server as shown in below figure then run
the CGI script.

• Python script first_CGI_script.py will run on localhost by


default.

© Jetking Infotrain Ltd. 2021


Apache server

© Jetking Infotrain Ltd. 2021


first_CGI_script.py

• Type the following path in to the web browser


localhost/python301/first_CGI_script.py

• It will display the following output.

© Jetking Infotrain Ltd. 2021


Web browser

© Jetking Infotrain Ltd. 2021


Quiz Time

• We need to start the ____ server then execute the CGI script.

A. Xampp
B. Apache
C. Wampp
D. None of the above

© Jetking Infotrain Ltd. 2021


Quiz Time

• The programs based on CGI helps in communicating between


the ___ and the client.

A. Python script
B. CGI script
C. web servers
D. None of the above

© Jetking Infotrain Ltd. 2021


Question for GD - (3 Minutes)

• Discuss about CGI programming.

© Jetking Infotrain Ltd. 2021


FieldStorage

• The cgi module defines a FieldStorage class.


• The FieldStorage class reads the form information from the
standard input or the environment.
• This is similar to Python's dictionary object consisting of form
elements and their respective values.
• We can use all dictionary function in the FieldStorage instance.

© Jetking Infotrain Ltd. 2021


FieldStorage

• Once the form data is retrieved by FieldStorage object on


server side, the Python script can perform required process and
render HTML output back to the client browser.
• The FieldStorage instance uses the many built-in methods to
manipulate the users' data.
• Below are a few FieldStorage's attributes and FieldStorage's
methods.

© Jetking Infotrain Ltd. 2021


FieldStorage attribute

© Jetking Infotrain Ltd. 2021


FieldStorage Methods

© Jetking Infotrain Ltd. 2021


Demonstration

• To understand CGI programming in python

© Jetking Infotrain Ltd. 2021


GET method
• The GET method sends the encoded user information that is
appended to the page request.
• The page and the encoded information are separated by the ?
Character.
https://localhost/path/get.py?key1=value1&key2=value2

• We can pass information by simply concatenating key and


value pairs along with any URL or you can use HTML
<FORM> tags to pass information using GET method.

© Jetking Infotrain Ltd. 2021


GET method
• The GET method is the default method to pass information from
browser to web server and it produces a long string that appears in
your browser's Location.
• Here is a simple URL, which passes two values to get.py program
using GET method.
localhost/cgi-bin/get.py?first_name=Narendra&last_name=Modi

• Below is the get.py script to handle input given by web browser. We are
going to use cgi module, which makes it very easy to access passed
information
© Jetking Infotrain Ltd. 2021
GET method

© Jetking Infotrain Ltd. 2021


GET method

© Jetking Infotrain Ltd. 2021


FORM Example

• Following example passes two values using HTML FORM and


submit button.

• We use same CGI script get.py to handle this input.

© Jetking Infotrain Ltd. 2021


HTML FORM : GET Method

© Jetking Infotrain Ltd. 2021


HTML FORM : GET Method

• The output of the above HTML form

© Jetking Infotrain Ltd. 2021


HTML FORM : GET Method
• Now enter First and Last Name and then click
submit button to see the result.

© Jetking Infotrain Ltd. 2021


HTML FORM : POST Method

• A generally more reliable method of passing information to a


CGI program is the POST method.
• This packages all the informations in exactly same way as GET
method, but instead of sending it as a text string after a ? in the
URL it sends the information as a separate message.
• The same get.py script is used which handles GET as well as
POST method.

© Jetking Infotrain Ltd. 2021


Quiz Time

• The cgi module defines a ______ class

A. FieldStorage
B. get
C. post
D. None of the above

© Jetking Infotrain Ltd. 2021


Quiz Time

• The GET method sends the _____ user information

A. Decoded
B. Encoded
C. New
D. Direct

© Jetking Infotrain Ltd. 2021


Quiz Time

• A generally more reliable method of passing information to a


CGI program is the ________ method.

A. Post
B. Get
C. All the above
D. None of the above

© Jetking Infotrain Ltd. 2021


Question for GD - (3 Minutes)

• Discuss FieldStorage method?

© Jetking Infotrain Ltd. 2021


HTML FORM : POST Method

© Jetking Infotrain Ltd. 2021


HTML FORM : POST Method

• The output of the above HTML form

© Jetking Infotrain Ltd. 2021


HTML FORM : POST Method
• Now enter First and Last Name and then click
submit button to see the result.

© Jetking Infotrain Ltd. 2021


Think, Pair And Share – 10 Minutes

• Discuss get & post method


• What is CGI script ?

© Jetking Infotrain Ltd. 2021


Summary

• Python CGI stands for Common Gateway Interface.


• The programs based on CGI helps in communicating between
the web servers and the client.
• Python CGI support for the uploading files through a HTML
form.
• The function of the cgi module stated above would help
throughout the script development.

© Jetking Infotrain Ltd. 2021


Summary

• Before start with CGI programming with python, first make


sure that your web server supports CGI and it is configured to
the handle CGI programs.
• XAMPP stands for cross-platform, Apache, MySQL, PHP,
and Perl, and it provides the localhost server to test or deploy
websites.
• The cgi module defines a FieldStorage class.

© Jetking Infotrain Ltd. 2021


Summary
• The FieldStorage class reads the form information from the standard input or
the environment.
• The GET method sends the encoded user information that is appended to the
page request.
• The GET method is the default method to pass information from browser to
web server and it produces a long string that appears in your browser's
Location.
• A generally more reliable method of passing information to a CGI program is
the POST method.

© Jetking Infotrain Ltd. 2021


Mind Map - (5 minutes)

• Draw a Mind Map to summarize the session.

© Jetking Infotrain Ltd. 2021


List of Labs

• Lab Activity:

– CGI script
– HTML form
– GET method

© Jetking Infotrain Ltd. 2021


Disclaimer

• The copyright of the images used in the slides lies with the
respective owner.
• Jetking does not claim ownership on any of the images used.
• This content is being used for educational and reference
purpose only.

© Jetking Infotrain Ltd. 2021

You might also like