SlideShare a Scribd company logo
Basic terms that we know
 Web Application
• A Web application (Web app) is an application
program that is stored on a remote server and
delivered over the Internet through a browser
interface.
 Web Server
• A web server is a computer system that processes
requests via HTTP, the basic network protocol
used to distribute information on the World Wide
Web.
 Web Browser
• A web browser is a software application for
retrieving, presenting and traversing
information resources on the World Wide Web.
 HTML
• HyperText Markup Language (HTML) is the
standard markup language for creating web
pages and web applications.
 Static Pages
• static Web pages are those with content that
cannot change without a developer editing its
source code,
 Dynamic Pages
• while dynamic Web pages can display different
content from the same source code.
What is HTTP?
 HTTP (Hypertext Transfer Protocol) is the set of rules
for transferring files (text, graphic images, sound,
video, and other multimedia files) on the World Wide
Web.
 As soon as a Web user opens their Web browser, the user
is indirectly making use of HTTP.
 HTTP is an application protocol that runs on top of the
TCP/IP suite of protocols (the foundation protocols for the
Internet).
 TCP/IP (Transmission Control Protocol/Internet Protocol) is
the basic communication language or protocol of the
Internet. It can also be used as a communications protocol
in a private network (either an intranet or an extranet).
CGI by rj
Get & Post Methods
 It specifies how to send the form data
to a web server.
 The data can be sent as URL
variables, by using the get (default)
method or as HTTP post, by using post
method.
Get method
 Get sends the data as part of the URL.
 Appends form-data into the URL in name/value pairs.
 The length of a URL is limited 2048 characters.
 Never use GET method if you have password or other sensitive
information to be sent to the server.
 Get is better for non secure data. Like query string in Google
(https://www.google.co.in/?
gfe_rd=cr&ei=KJWNWJ_lKKnT8ge0gYjQBw#safe=strict&q=what+i
s+php)
 Get can’t be used to send binary data. Ex. Images or word
documents.
 <form action=“myform.php” method=“GET”/>
Post Method
 http post requests supply additional data form the client (browser)
to the server in the message body.
 Appends form-data inside the body of the HTTP request (data is not
shown is in URL)
 Don’t have any restriction on data size to be sent.
 Post can not be bookmarked.
 Used to send ASCII as well as binary data. (img, word document)
 Is safe because the parameters are not stored in browser history or
in the web server logs.
 <from action=“some.php” method=“post”/>
CGI by rj
What is CGI?
Web Browsing
 To understand the concept of CGI, let us see what happens
when we 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 parses the URL and looks for the filename.
 If it finds that file then sends it back to the browser,
otherwise sends an error message indicating that you
requested a wrong file.
 Web browser takes response from web server and displays
either the received file or error message.
What is CGI?
 However, it is possible to set up the HTTP server so that
whenever a file in a certain directory is requested that
file is not sent back;
 instead it is executed as a program, and whatever that
program outputs is sent back for your browser to display.
 This function is called the Common Gateway Interface or
CGI and the programs are called CGI scripts.
 CGI standard defines server-program interaction
• Developed at the National Center for Supercomputing Applications
(NCSA)
 􀂄 CGI was the first way of generating dynamic content.
 These CGI programs can be a Python Script, PERL
Script, Shell Script, C or C++ program, etc.
CGI by rj
WWW
Client
CGI
program
WWW
Server
request
response
Invoke CGI
CGI output
internet server
How to Run Python on
XAMPP web server
 you have installed both xampp and
python on the C Drive:
 c:/xampp
 c:/python27 or c:/python35
Running Python Scripts via
CGI
 Modify http.conf
 Locate the configuration file http.conf in
c:xamppapacheconfhttpd.conf.
 In http.conf look for this line: AddHandler cgi-script
.cgi .pl .asp.
 Modify it so it looks like this: AddHandler cgi-script .cgi .pl
.asp .py
 You probably won't need to do this, but just to be sure, look
for this line: Options Indexes FollowSymLinks.
 Ensure that it looks like this: Options Indexes
FollowSymLinks Includes ExecCGI
 Restart Apache
 At the top of each Python script you create,
set the path to your version of Python.
 For instance, ours is in C:Python27 so we
write: #!/Python27/python
 Example script, create folder
at C:xampphtdocs and create script file
with .cgi extension containing the following
code within the folder:-
Simple CGI Application
#!/Python27/python.exe
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>'
How to process html form data
with Python
 Create a file named "process_form" with the
code below and save it in a file with .py or
.cgi extension at this location
"C:xampphtdocscgirj" where the html form
file (myForm.html) above is located.
 Edit the html form "action" attribute to point to
the (cgi or py) python file above
(process_form.cgi).
CGI module
 There is one primary class in the cgi module that does all
the work: the FieldStorage class.
 This class should be instantiated when a Python CGI script
begins, as it will read in all the pertinent user information
from the Web client (via the Web server).
 Once this object has been instantiated, it will consist of a
dictionary-like object that has a set of key-value pairs.
 The keys are the names of the form items that were
passed in through the form while the values contain the
corresponding data.
 These values themselves can be one of three objects.
 They can be FieldStorage objects (instances) as well as
instances of a similar class called MiniFieldStorage, which
is used in cases where no file uploads or multiple-part form
data is involved.
 MiniFieldStorage instances contain only the key-value pair
of the name and the data. Lastly, they can be a list of such
objects.
 This occurs when a form contains more than one input item
with the same field name.
Cgitb module
 The cgitb module provides a special exception handler for Python
scripts. (Its name is a bit misleading. It was originally designed to display
extensive traceback information in HTML for CGI scripts. It was later
generalized to also display this information in plain text.)
 After this module is activated, if an uncaught exception occurs, a
detailed, formatted report will be displayed.
 The report includes a traceback showing excerpts of the source code for
each level, as well as the values of the arguments and local variables to
currently running functions, to help you debug the problem.
 Optionally, you can save this information to a file instead of sending it to
the browser.
 To enable this feature, simply add this to the top of your CGI script:
 import cgitb
 cgitb.enable()
Field storage
 To access the data submitted by the form in our script we
will need to make use of the FieldStorage() function
available in the cgi module of Python.
 For example, we would assign the data from the form to a
Python variable in the following way:
 formData = cgi.FieldStorage()
 The variable formData now contains every piece of
data submitted by the form.
 Other tool that provide Apache web
server on PC are WAMP or AMPPS.
 AMPPS is a stack of Apache, MySQL,
MongoDB, PHP, Perl & Python.
CGI: Pros and Cons
 Pros of CGI:
• Simple; suitable for small once-off tasks
• Supported by all web servers
 Cons of CGI:
• Slow; web server forks new process for every
request
• Parameter decoding tedious
Advanced CGI
Multi part form submission,
File upload,
Cookies
Cookie
 Cookies are basically key-value pairs
that a web server can set for a client's
web browser.
 This lets server programs track what
pages a user has visited or what actions
the user has performed. Cookies are
very useful for implementing things like
log-in sessions or shopping carts.
Using Cookies in CGI
 HTTP protocol is a stateless protocol. For a commercial
website, it is required to maintain session information among
different pages.
 For example, one user registration ends after completing
many pages. How to maintain user's session information
across all the web pages?
 In many situations, using cookies is the most efficient
method of remembering and tracking preferences,
purchases, commissions, and other information required for
better visitor experience or site statistics.
How It Works?
 Your server sends some data to the visitor's browser in the
form of a cookie.
 The browser may accept the cookie.
 If it does, it is stored as a plain text record on the visitor's
hard drive.
 Now, when the visitor arrives at another page on your site,
the cookie is available for retrieval. Once retrieved, your
server knows/remembers what was stored.
 The HTTP headers are a series of text parameters sparated
by carriage return and line feed characters.
 Cookies are set using the Set-Cookie parameter.
 Cookies are a plain text data record of 5 variable-length fields:
 Expires: The date the cookie will expire. If this is blank, the cookie will
expire when the visitor quits the browser.
 Domain: The domain name of your site.
 Path: The path to the directory or web page that sets the cookie. This
may be blank if you want to retrieve the cookie from any directory or
page.
 Secure: If this field contains the word "secure", then the cookie may
only be retrieved with a secure server. If this field is blank, no such
restriction exists.
 Name=Value: Cookies are set and retrieved in the form of key and
value pairs.
Setting up Cookies
 It is very easy to send cookies to browser.
 These cookies are sent along with HTTP
Header before to Content-type field.
 Assuming you want to set UserID and
Password as cookies.
File Upload Example
 To upload a file, the HTML form must
have the enctype attribute set to
multipart/form-data.
 The input tag with the file type creates a
"Browse" button.
CGI by rj

More Related Content

PPT
Excellent rest using asp.net web api
PPT
Lecture 1
PPT
Lecture 1
PPTX
Web technologies: HTTP
PDF
21 HTTP Protocol #burningkeyboards
PPTX
The Full Power of ASP.NET Web API
PDF
HTTP & HTML & Web
PPTX
HTTP request and response
Excellent rest using asp.net web api
Lecture 1
Lecture 1
Web technologies: HTTP
21 HTTP Protocol #burningkeyboards
The Full Power of ASP.NET Web API
HTTP & HTML & Web
HTTP request and response

What's hot (20)

PPTX
ASP.NET Web API and HTTP Fundamentals
PPTX
0 csc 3311 slide internet programming
PPTX
Lecture 6- http
KEY
What's up with HTTP?
PPTX
PPT
Html intake 38 lect1
PPTX
Www and http
PPTX
Http-protocol
PPTX
Http protocol
PPTX
PPTX
Http headers
PPTX
The ASP.NET Web API for Beginners
PPTX
SCWCD : The web client model
PPT
Hypertext transfer protocol (http)
PPTX
Hypertex transfer protocol
PPTX
HTTP Protocol Basic
PPTX
Overview of Rest Service and ASP.NET WEB API
PPT
Introduction About PHP
PPTX
SCWCD : The web client model : CHAP : 1
PDF
ASP.NET Web API and HTTP Fundamentals
0 csc 3311 slide internet programming
Lecture 6- http
What's up with HTTP?
Html intake 38 lect1
Www and http
Http-protocol
Http protocol
Http headers
The ASP.NET Web API for Beginners
SCWCD : The web client model
Hypertext transfer protocol (http)
Hypertex transfer protocol
HTTP Protocol Basic
Overview of Rest Service and ASP.NET WEB API
Introduction About PHP
SCWCD : The web client model : CHAP : 1
Ad

Similar to CGI by rj (20)

PPT
Spsl v unit - final
PPT
Fm 2
PDF
DOCX
Copy of cgi
PPT
CGI Presentation
PDF
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
PPT
Common gateway interface
DOC
Perl web programming
PPTX
Common Gateway Interface ppt
PDF
How cgi scripting works
PDF
Unit 02: Web Technologies (2/2)
PDF
Slides serverside main
ODP
Introduction to Web Programming with Perl
PPTX
Web Techniques like Cookies and Sessions
ODP
PHP Training: Module 1
PDF
Configuring the Apache Web Server
Spsl v unit - final
Fm 2
Copy of cgi
CGI Presentation
[DSBW Spring 2009] Unit 02: Web Technologies (2/2)
Common gateway interface
Perl web programming
Common Gateway Interface ppt
How cgi scripting works
Unit 02: Web Technologies (2/2)
Slides serverside main
Introduction to Web Programming with Perl
Web Techniques like Cookies and Sessions
PHP Training: Module 1
Configuring the Apache Web Server
Ad

More from Shree M.L.Kakadiya MCA mahila college, Amreli (20)

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Institutional Correction lecture only . . .
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Business Ethics Teaching Materials for college
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning
Institutional Correction lecture only . . .
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
Module 4: Burden of Disease Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial diseases, their pathogenesis and prophylaxis
Business Ethics Teaching Materials for college
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf

CGI by rj

  • 1. Basic terms that we know  Web Application • A Web application (Web app) is an application program that is stored on a remote server and delivered over the Internet through a browser interface.  Web Server • A web server is a computer system that processes requests via HTTP, the basic network protocol used to distribute information on the World Wide Web.
  • 2.  Web Browser • A web browser is a software application for retrieving, presenting and traversing information resources on the World Wide Web.  HTML • HyperText Markup Language (HTML) is the standard markup language for creating web pages and web applications.
  • 3.  Static Pages • static Web pages are those with content that cannot change without a developer editing its source code,  Dynamic Pages • while dynamic Web pages can display different content from the same source code.
  • 4. What is HTTP?  HTTP (Hypertext Transfer Protocol) is the set of rules for transferring files (text, graphic images, sound, video, and other multimedia files) on the World Wide Web.  As soon as a Web user opens their Web browser, the user is indirectly making use of HTTP.  HTTP is an application protocol that runs on top of the TCP/IP suite of protocols (the foundation protocols for the Internet).  TCP/IP (Transmission Control Protocol/Internet Protocol) is the basic communication language or protocol of the Internet. It can also be used as a communications protocol in a private network (either an intranet or an extranet).
  • 6. Get & Post Methods  It specifies how to send the form data to a web server.  The data can be sent as URL variables, by using the get (default) method or as HTTP post, by using post method.
  • 7. Get method  Get sends the data as part of the URL.  Appends form-data into the URL in name/value pairs.  The length of a URL is limited 2048 characters.  Never use GET method if you have password or other sensitive information to be sent to the server.  Get is better for non secure data. Like query string in Google (https://www.google.co.in/? gfe_rd=cr&ei=KJWNWJ_lKKnT8ge0gYjQBw#safe=strict&q=what+i s+php)  Get can’t be used to send binary data. Ex. Images or word documents.  <form action=“myform.php” method=“GET”/>
  • 8. Post Method  http post requests supply additional data form the client (browser) to the server in the message body.  Appends form-data inside the body of the HTTP request (data is not shown is in URL)  Don’t have any restriction on data size to be sent.  Post can not be bookmarked.  Used to send ASCII as well as binary data. (img, word document)  Is safe because the parameters are not stored in browser history or in the web server logs.  <from action=“some.php” method=“post”/>
  • 10. What is CGI? Web Browsing  To understand the concept of CGI, let us see what happens when we 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 parses the URL and looks for the filename.  If it finds that file then sends it back to the browser, otherwise sends an error message indicating that you requested a wrong file.  Web browser takes response from web server and displays either the received file or error message.
  • 11. What is CGI?  However, it is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back;  instead it is executed as a program, and whatever that program outputs is sent back for your browser to display.  This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts.  CGI standard defines server-program interaction • Developed at the National Center for Supercomputing Applications (NCSA)  􀂄 CGI was the first way of generating dynamic content.  These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program, etc.
  • 14. How to Run Python on XAMPP web server  you have installed both xampp and python on the C Drive:  c:/xampp  c:/python27 or c:/python35
  • 15. Running Python Scripts via CGI  Modify http.conf  Locate the configuration file http.conf in c:xamppapacheconfhttpd.conf.  In http.conf look for this line: AddHandler cgi-script .cgi .pl .asp.  Modify it so it looks like this: AddHandler cgi-script .cgi .pl .asp .py  You probably won't need to do this, but just to be sure, look for this line: Options Indexes FollowSymLinks.  Ensure that it looks like this: Options Indexes FollowSymLinks Includes ExecCGI  Restart Apache
  • 16.  At the top of each Python script you create, set the path to your version of Python.  For instance, ours is in C:Python27 so we write: #!/Python27/python  Example script, create folder at C:xampphtdocs and create script file with .cgi extension containing the following code within the folder:-
  • 17. Simple CGI Application #!/Python27/python.exe 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>'
  • 18. How to process html form data with Python  Create a file named "process_form" with the code below and save it in a file with .py or .cgi extension at this location "C:xampphtdocscgirj" where the html form file (myForm.html) above is located.  Edit the html form "action" attribute to point to the (cgi or py) python file above (process_form.cgi).
  • 19. CGI module  There is one primary class in the cgi module that does all the work: the FieldStorage class.  This class should be instantiated when a Python CGI script begins, as it will read in all the pertinent user information from the Web client (via the Web server).  Once this object has been instantiated, it will consist of a dictionary-like object that has a set of key-value pairs.  The keys are the names of the form items that were passed in through the form while the values contain the corresponding data.
  • 20.  These values themselves can be one of three objects.  They can be FieldStorage objects (instances) as well as instances of a similar class called MiniFieldStorage, which is used in cases where no file uploads or multiple-part form data is involved.  MiniFieldStorage instances contain only the key-value pair of the name and the data. Lastly, they can be a list of such objects.  This occurs when a form contains more than one input item with the same field name.
  • 21. Cgitb module  The cgitb module provides a special exception handler for Python scripts. (Its name is a bit misleading. It was originally designed to display extensive traceback information in HTML for CGI scripts. It was later generalized to also display this information in plain text.)  After this module is activated, if an uncaught exception occurs, a detailed, formatted report will be displayed.  The report includes a traceback showing excerpts of the source code for each level, as well as the values of the arguments and local variables to currently running functions, to help you debug the problem.  Optionally, you can save this information to a file instead of sending it to the browser.  To enable this feature, simply add this to the top of your CGI script:  import cgitb  cgitb.enable()
  • 22. Field storage  To access the data submitted by the form in our script we will need to make use of the FieldStorage() function available in the cgi module of Python.  For example, we would assign the data from the form to a Python variable in the following way:  formData = cgi.FieldStorage()  The variable formData now contains every piece of data submitted by the form.
  • 23.  Other tool that provide Apache web server on PC are WAMP or AMPPS.  AMPPS is a stack of Apache, MySQL, MongoDB, PHP, Perl & Python.
  • 24. CGI: Pros and Cons  Pros of CGI: • Simple; suitable for small once-off tasks • Supported by all web servers  Cons of CGI: • Slow; web server forks new process for every request • Parameter decoding tedious
  • 25. Advanced CGI Multi part form submission, File upload, Cookies
  • 26. Cookie  Cookies are basically key-value pairs that a web server can set for a client's web browser.  This lets server programs track what pages a user has visited or what actions the user has performed. Cookies are very useful for implementing things like log-in sessions or shopping carts.
  • 27. Using Cookies in CGI  HTTP protocol is a stateless protocol. For a commercial website, it is required to maintain session information among different pages.  For example, one user registration ends after completing many pages. How to maintain user's session information across all the web pages?  In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.
  • 28. How It Works?  Your server sends some data to the visitor's browser in the form of a cookie.  The browser may accept the cookie.  If it does, it is stored as a plain text record on the visitor's hard drive.  Now, when the visitor arrives at another page on your site, the cookie is available for retrieval. Once retrieved, your server knows/remembers what was stored.  The HTTP headers are a series of text parameters sparated by carriage return and line feed characters.  Cookies are set using the Set-Cookie parameter.
  • 29.  Cookies are a plain text data record of 5 variable-length fields:  Expires: The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.  Domain: The domain name of your site.  Path: The path to the directory or web page that sets the cookie. This may be blank if you want to retrieve the cookie from any directory or page.  Secure: If this field contains the word "secure", then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.  Name=Value: Cookies are set and retrieved in the form of key and value pairs.
  • 30. Setting up Cookies  It is very easy to send cookies to browser.  These cookies are sent along with HTTP Header before to Content-type field.  Assuming you want to set UserID and Password as cookies.
  • 31. File Upload Example  To upload a file, the HTML form must have the enctype attribute set to multipart/form-data.  The input tag with the file type creates a "Browse" button.