 2002 Prentice Hall. All rights reserved. 1 Chapter 6 – Introduction to the Common Gateway Interface (CGI) Outline 6.1 Introduction 6.2 Client and Web.

Slides:



Advertisements
Similar presentations
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Advertisements

CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
DT228/3 Web Development WWW and Client server model.
Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 21 – Web Servers (IIS and Apache) Outline 21.1 Introduction 21.2 HTTP Request Types 21.3 System.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 21 – Web Servers (IIS and Apache) Outline 21.1 Introduction 21.2 HTTP Request Types 21.3 System.
Web Servers How do our requests for resources on the Internet get handled? Can they be located anywhere? Global?
Python and Web Programming
Definitions, Definitions, Definitions Lead to Understanding.
 2008 Pearson Education, Inc. All rights reserved Web Servers (IIS and Apache)
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Web Client/Server Communication A290/A590, Fall /09/2014.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
17 Web Servers (Apache and IIS)
1 Web Servers (IIS and Apache) Outline 9.1 Introduction 9.2 HTTP Request Types 9.3 System Architecture 9.4 Client-Side Scripting versus Server-Side Scripting.
1 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
INTRODUCTION TO WEB DATABASE PROGRAMMING
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
Overview A plain HTML document is static A CGI program is executed in real-time, so that it can output dynamic information. CGI (Common Gateway Interface)
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 29.1 Multi-Tier Architecture 29.2 Web Servers.
1 Web Server Concepts Dr. Awad Khalil Computer Science Department AUC.
1 HTML and CGI Scripting CSC8304 – Computing Environments for Bioinformatics - Lecture 10.
Copyright © cs-tutorial.com. Introduction to Web Development In 1990 and 1991,Tim Berners-Lee created the World Wide Web at the European Laboratory for.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 1: Introduction to Web
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
CP476 Internet Computing CGI1 CGI is a common way to provide for specific computations on server side, interactions with users, or access to databases.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Web Servers (IIS, PWS and Apache) Outline 21.1 Introduction 21.2 HTTP Request Types 21.3.
USING PERL FOR CGI PROGRAMMING
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: HTTP and CGI Fundamentals of Web Programming.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Website Development with PHP and MySQL Saving Data.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Chapter.
Chapter 9: Perl and CGI Programming CGI Programming Acknowledgement: Some materials are taken from Teach Yourself CGI Programming with PERL 5 in a Week.
1 Welcome to CSC 301 Web Programming Charles Frank.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
1 Web Servers (Chapter 21 – Pages( ) Outline 21.1 Introduction 21.2 HTTP Request Types 21.3 System Architecture.
Chapter 29 World Wide Web & Browsing World Wide Web (WWW) is a distributed hypermedia (hypertext & graphics) on-line repository of information that users.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
 2008 Pearson Education, Inc. All rights reserved. Chapter 17 Web Servers (Apache and IIS) Internet & World Wide Web How to Program, 5/e.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Module: Software Engineering of Web Applications Chapter 2: Technologies 1.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
Introduction and Principles Web Server Scripting.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
JavaScript and Ajax (Internet Background) Week 1 Web site:
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Copyright © 2011 Pearson Addison-Wesley Web Servers and Projects Deployment Chapter 11 – Slide 1.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Web Development Web Servers.
IS333D: MULTI-TIER APPLICATION DEVELOPMENT
Web Servers (IIS and Apache)
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 6 – Introduction to the Common Gateway Interface (CGI) Outline 6.1 Introduction 6.2 Client and Web Server Interaction System Architecture 6.2.2Accessing Web Servers HTTP Transactions 6.3 Simple CGI Script 6.4Sending Input to a CGI Script 6.5Using XHTML Forms to Send Input and Using Module cgi to Retrieve Form Data 6.6Using cgi.FieldStorage to Read Input 6.7Other HTTP Headers 6.8 Example: Interactive Portal 6.9Internet and World Wide Web Resources

 2002 Prentice Hall. All rights reserved. 2 Common Gateway Interface (CGI) Web-page generation –Dynamic : content generated each request –Static : never changes unless document edited 6.1 Introduction

 2002 Prentice Hall. All rights reserved. 3 Extensible Hypertext Markup Language (XHTML) –Documents contain markup, or tags –Requires syntactically correct documents Uniform Resource Locator (URL) directs browser to resource Hypertext Transfer Protocol (HTTP) for transferring requests and files over the Internet Domain name system (DNS) server translates hostname into Internet Protocol (IP) address 6.2 Client and Web Server Interaction

 2002 Prentice Hall. All rights reserved. 4 Multi-tier applications –Information tier Also called data tier or bottom tier Maintains data for application –Middle tier Implements presentation logic and enforces business rules Controller logic processes client requests and retrieves data –Client tier Also called top tier Application’s user interface System Architecture

 2002 Prentice Hall. All rights reserved System Architecture Fig. 6.1Three-tier application model.

 2002 Prentice Hall. All rights reserved Accessing Web Servers Request documents from local or remote Web servers Ways to request a document –Local Web server : machine name or localhost –Remote Web server: specify server’s domain name or IP address Domain name –Represents groups of hosts on the Internet –combines with top-level domain (TLD) to form fully qualified hostname

 2002 Prentice Hall. All rights reserved. 7 HTTP request types –Get : sends form content as part of URL –Post : users cannot see sent data HTTP headers –provide additional information about data sent in response to request –Multipurpose Internet Mail Extensions (MIME) : Internet standard that specifies how messages should be formatted HTTP Transactions

 2002 Prentice Hall. All rights reserved HTTP Transactions Fig. 6.2Client interacting with server and Web server. Step 1: The request, GET /books/downloads.html HTTP/1.1.

 2002 Prentice Hall. All rights reserved HTTP Transactions Fig. 6.2Client interacting with server and Web server. Step 2: The HTTP response, HTTP/ OK.

 2002 Prentice Hall. All rights reserved. 10 Two types of scripting –Server-side scripting (i.e., CGI scripts) manipulate server resources –Client-side scripting (i.e, JavaScript) accesses browser features, manipulates browser documents, validates user input, etc. Server-side scripts –Execute on server –Usually generate custom responses for clients –Wider range of programmatic capabilities than client-side scripts Simple CGI Script

 2002 Prentice Hall. All rights reserved. Outline 11 fig06_03.py 1 #!c:\Python\python.exe 2 # Fig. 6.3: fig06_03.py 3 # Displays current date and time in Web browser. 4 5 import time 6 7 def printHeader( title ): 8 print """Content-type: text/html <!DOCTYPE html PUBLIC 12 "-//W3C//DTD XHTML 1.0 Strict//EN" 13 "DTD/xhtml1-strict.dtd"> %s """ % title printHeader( "Current date and time" ) 20 print time.ctime( time.time() ) 21 print " " Directive specifies location of server’s Python interpreter Print HTTP header Blank line signifies end of HTTP header Print XML declaration Print XHTML document headerReturns seconds since epoch Formats seconds to human-readable time

 2002 Prentice Hall. All rights reserved Simple CGI Script Fig. 6.4Step 1: The GET request, GET /cgi-bin/fig06_02.py HTTP/1.1. (Part 1 of 4.)

 2002 Prentice Hall. All rights reserved. 13 Fig. 6.4Step 2: The Web server starts the CGI script. (Part 2 of 4.) 6.3 Simple CGI Script

 2002 Prentice Hall. All rights reserved. 14 Fig. 6.4Step 3: The output of the script is sent to the Web server. (Part 3 of 4.) 6.3 Simple CGI Script

 2002 Prentice Hall. All rights reserved. 15 Fig. 6.4Step 4: The HTTP response, HTTP/ OK. (Part 4 of 4.) 6.3 Simple CGI Script

 2002 Prentice Hall. All rights reserved. Outline 16 fig06_05.py 1 #!c:\Python\python.exe 2 # Fig. 6.5: fig06_05.py 3 # Program displaying CGI environment variables. 4 5 import os 6 import cgi 7 8 def printHeader( title ): 9 print """Content-type: text/html <!DOCTYPE html PUBLIC 13 "-//W3C//DTD XHTML 1.0 Strict//EN" 14 "DTD/xhtml1-strict.dtd"> %s """ % title rowNumber = 0 21 backgroundColor = "white" printHeader( "Environment Variables" ) 24 print """ """ # print table of cgi variables and values 27 for item in os.environ.keys(): 28 rowNumber += if rowNumber % 2 == 0: # even row numbers are white 31 backgroundColor = "white" 32 else: # odd row numbers are grey 33 backgroundColor = "lightgrey" 34 Module cgi provides form processing and text formatting capabilitiesModule os provides access to environment variablesStart table with tag os.environ acts like a dictionary of environment variables and values

 2002 Prentice Hall. All rights reserved. Outline 17 fig06_05.py 35 print """ 36 %s %s """ % ( backgroundColor, 37 cgi.escape( item ), cgi.escape( os.environ[ item ] ) ) print """ """ Table row for each environment variables and its value Function cgi.escape takes a string and returns a properly formatted XHMTL string Close table, body of XHMTL document and XHTML document

 2002 Prentice Hall. All rights reserved Sending Input to a CGI Script QUERY_STRING variable contains extra information appended to a URL in a GET request, following a question mark ( ? ) Question mark ( ? ) serves as delimiter between source and query string Name-value pairs in the query string separated by ampersands ( & )

 2002 Prentice Hall. All rights reserved. Outline 19 fig06_06.py 1 #!c:\Python\python.exe 2 # Fig. 6.6: fig06_06.py 3 # Example using QUERY_STRING. 4 5 import os 6 import cgi 7 8 def printHeader( title ): 9 print """Content-type: text/html <!DOCTYPE html PUBLIC 13 "-//W3C//DTD XHTML 1.0 Strict//EN" 14 "DTD/xhtml1-strict.dtd"> %s """ % title printHeader( "QUERY_STRING example" ) 21 print " Name/Value Pairs " query = os.environ[ "QUERY_STRING" ] if len( query ) == 0: 26 print """ 27 Please add some name-value pairs to the URL above. 28 Or try 29 this. 30 """ 31 else: 32 print """ 33 The query string is '%s'. """ % cgi.escape( query ) 34 pairs = cgi.parse_qs( query ) 35 Contains information appended to URL in GET request Parses query string and returns dictionary of its name-value pairs

 2002 Prentice Hall. All rights reserved. Outline 20 fig06_06.py 36 for key, value in pairs.items(): 37 print " You set '%s' to value %s "" % \ 38 ( key, value ) print " "

 2002 Prentice Hall. All rights reserved. Outline 21 fig06_06.py

 2002 Prentice Hall. All rights reserved Using XHMTL Forms to Send Input and Using Module cgi to Retrieve Form Data Web page forms provide an intuitive way for users to input information to CGI scripts and tags surround an XHTML form –Attribute action specifies operation to perform when users submit the form –Attribute method : either get or post Get sends data to CGI script via QUERY_STRING environment variable Post sends data to CGI script via standard input and sets environment variable CONTENT_LENGTH to number of characters that were posted

 2002 Prentice Hall. All rights reserved Using XHMTL Forms to Send Input and Using Module cgi to Retrieve Form Data

 2002 Prentice Hall. All rights reserved Using XHMTL Forms to Send Input and Using Module cgi to Retrieve Form Data

 2002 Prentice Hall. All rights reserved. Outline 25 fig06_08.py 1 #!c:\Python\python.exe 2 # Fig. 6.8: fig06_08.py 3 # Demonstrates get method with an XHTML form. 4 5 import cgi 6 7 def printHeader( title ): 8 print """Content-type: text/html <!DOCTYPE html PUBLIC 12 "-//W3C//DTD XHTML 1.0 Strict//EN" 13 "DTD/xhtml1-strict.dtd"> %s """ % title printHeader( "Using 'get' with forms" ) 20 print """ Enter one of your favorite words here: """ pairs = cgi.parse() if pairs.has_key( "word" ): 31 print """ Your word is: 32 %s """ \ 33 % cgi.escape( pairs[ "word" ][ 0 ] ) print " " Parse form data and return a dictionary Input data keyed by name attribute of input element Called in case form data includes special characters

 2002 Prentice Hall. All rights reserved. Outline 26 fig06_08.py

 2002 Prentice Hall. All rights reserved. Outline 27 fig06_09.py 1 #!c:\Python\python.exe 2 # Fig. 6.9: fig06_09.py 3 # Demonstrates post method with an XHTML form. 4 5 import cgi 6 7 def printHeader( title ): 8 print """Content-type: text/html <!DOCTYPE html PUBLIC 12 "-//W3C//DTD XHTML 1.0 Strict//EN" 13 "DTD/xhtml1-strict.dtd"> %s """ % title printHeader( "Using 'post' with forms" ) 20 print """ Enter one of your favorite words here: """ pairs = cgi.parse() if pairs.has_key( "word" ): 31 print """ Your word is: 32 %s """ \ 33 % cgi.escape( pairs[ "word" ][ 0 ] ) print " " Post request attaches form content to the end of a HTTP request

 2002 Prentice Hall. All rights reserved. Outline 28 fig06_09.py

 2002 Prentice Hall. All rights reserved. 29 Module cgi provides class FieldStorage to parse forms 6.5 Using cgi.FieldStorage to Read Input

 2002 Prentice Hall. All rights reserved. Outline 30 fig06_10.py 1 #!c:\Python\python.exe 2 # Fig. 6.10: fig06_10.py 3 # Demonstrates use of cgi.FieldStorage an with XHTML form. 4 5 import cgi 6 7 def printHeader( title ): 8 print """Content-type: text/html <!DOCTYPE html PUBLIC 12 "-//W3C//DTD XHTML 1.0 Strict//EN" 13 "DTD/xhtml1-strict.dtd"> %s """ % title printHeader( "Using cgi.FieldStorage with forms" ) 20 print """ Enter one of your favorite words here: """ form = cgi.FieldStorage() if form.has_key( "word" ): 31 print """ Your word is: 32 %s """ \ 33 % cgi.escape( form[ "word" ].value ) print " " cgi.FieldStorage object stores form data in a dictionaryForm data keyed by the value of each input element’s name attribute

 2002 Prentice Hall. All rights reserved. Outline 31 fig06_10.py

 2002 Prentice Hall. All rights reserved. 32 Content-type header specifies how document is processed Refresh header redirects client to new location or refreshes current page Location header indicates redirection performed on server side Status header tells server to output status- header line 6.5 Other HTTP Headers

 2002 Prentice Hall. All rights reserved. Outline 33 fig06_11.html 1 2 <!DOCTYPE html PUBLIC 3 "-//W3C//DTD XHTML 1.0 Strict//EN" 4 "DTD/xhtml1-strict.dtd"> Enter here Welcome to Bug2Bug Travel Please enter your name: Members, please enter the password: Note that password is not encrypted CGI script called when user submits form

 2002 Prentice Hall. All rights reserved. Outline 34 fig06_11.html

 2002 Prentice Hall. All rights reserved. Outline 35 fig06_12.py 1 #!c:\Python\python.exe 2 # Fig. 6.12: fig06_12.py 3 # Handles entry to Bug2Bug Travel. 4 5 import cgi 6 7 def printHeader( title ): 8 print """Content-type: text/html <!DOCTYPE html PUBLIC 12 "-//W3C//DTD XHTML 1.0 Strict//EN" 13 "DTD/xhtml1-strict.dtd"> %s """ % title form = cgi.FieldStorage() if not form.has_key( "name" ): 22 print "Location: /fig06_11.html\n" 23 else: 24 printHeader( "Bug2Bug Travel" ) 25 print " Welcome, %s! " % form[ "name" ].value 26 print """ Here are our weekly specials: 27 Boston to Taiwan for $300 """ if not form.has_key( "password" ): 30 print """ 31 Become a member today for more great deals! """ 32 elif form[ "password" ].value == "Coast2Coast": 33 print """ 34 Current specials just for members: 35 San Diego to Hong Kong for $250 """ User did not enter login nameUser entered password

 2002 Prentice Hall. All rights reserved. Outline 36 fig06_12.py