Python CGI programming

Slides:



Advertisements
Similar presentations
Introduction to Web Design Lecture number:. Todays Aim: Introduction to Web-designing and how its done. Modelling websites in HTML.
Advertisements

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?
Learning HTML. > Title of page This is my first homepage. Tells Browser This is an HTML page Basic Tags Tells Browser End of HTML page Header information.
HyperText Markup Language (HTML). Introduction to HTML Hyper Text Markup Language HTML Example The structure of an HTML document Agenda.
WeB application development
Website Design.
Hypertext Markup Language. Platform: - Independent  This means it can be interpreted on any computer regardless of the hardware or operating system.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Tutorial 6 Working with Web Forms
Forms Describe common uses of forms on web pages
Python and Web Programming
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
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.
Chapter 2 Introduction to HTML5 Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
XP Tutorial 6New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Creating Web Page Forms Designing a Product Registration Form Tutorial.
XP Tutorial 6New Perspectives on HTML and XHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Chapter 16 The World Wide Web Chapter Goals ( ) Compare and contrast the Internet and the World Wide Web Describe general Web processing.
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
Chapter 16 The World Wide Web. 2 The Web An infrastructure of information combined and the network software used to access it Web page A document that.
HTML II. Factors to consider in designing a website. Organizing your files. HTML Tables. Unordered Lists. Ordered Lists. HTML Forms. Learning Objectives.
1 HTML References: A HTML Tutorial: /HTMLPrimer.html
XHTML Introductory1 Linking and Publishing Basic Web Pages Chapter 3.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Understanding HTML Code
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
Introduction to HTML. What is a HTML File?  HTML stands for Hyper Text Markup Language  An HTML file is a text file containing small markup tags  The.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
INTRODUCTION. What is HTML? HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language HTML is not a programming language,
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
Website Development with PHP and MySQL Saving Data.
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
1 © Copyright 2000 Ethel Schuster The Web… in 15 minutes Ethel Schuster
Lec.10 + (Chapter 8 & 9) GUI Java Applet Jiang (Jen) ZHENG July 6 th, 2005.
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.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Computational Boot Camp HTML Mike Schaffer. 8/23/2002 (MES) HTML What is HTML? HTML stands for HyperText Markup Language HTML is the language for publishing.
HTML BTEC National in Computing Section5. Create Information “HTML: defining HTML, discussing HTML uses and demonstrating HTML basics, HTML structure…..
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
HTLM Forms CS3505. Form Handling in Browser html User Files out form WEbBROWSErWEbBROWSEr User read response submit Get URL?input html Get file html script.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
HTML – The Basics COE 201- Computer Proficiency. The Internet The World Wide Web ▫Also known as the Web ▫Created in 1989  European Laboratory for Particle.
What is HTML? HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language HTML is not a programming language, it is a markup.
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
CHAPTER TWO HTML TAGS. 1.Basic HTML Tags 1.1 HTML: Hypertext Markup Language  HTML stands for Hypertext Markup Language.  It is the markup language.
HTML-I Basic HTML Elements. HTML (Hyper Text Markup Language) HTML is a document layout and hyperlink- specification language. i.e. a language used to.
HTML. INDEX Introduction to HTML Creating Web Pages Commands And Tags Web Page.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
HTML And the Internet. HTML and the Internet ► HTML: HyperText Markup Language  Language in which all pages on the web are written  Not Really a Programming.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
XP Tutorial 6New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
Introduction to HTML.
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
Introduction to XHTML.
Chapter 27 WWW and HTTP.
Chapter 16 The World Wide Web.
Presentation transcript:

Python CGI programming

CGI Programming The Common Gateway Interface, or CGI, is a set of standards that define how information is exchanged between the web server and a custom script or program. To understand the concept of CGI, lets 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 asks for the URL ie. filename. Web Server will parse the URL and will look for the filename, if it finds that file it sends to the browser otherwise sends an error message indicating that file could not be found . Web browser takes response from web 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 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. When we run a Python program as a CGI script, the output goes out directly and the file must be executable (+x). The first line we have to print are the HTTP headers – this tells the browser to read it as either text, or render it as HTML. Any line after that comprises the actual HTML or text itself

CGI Architecture diagram

What is an HTML File? HTML stands for Hyper Text Markup Language An HTML file is a text file containing small markup tags The markup tags tell the Web browser how to display the page An HTML file must have an htm or html file extension An HTML file can be created using a simple text editor

HTML Elements HTML documents are text files made up of HTML elements. HTML elements are defined using HTML tags. HTML Tags HTML tags are used to mark-up HTML elements HTML tags are surrounded by the two characters < and > The surrounding characters are called angle brackets HTML tags normally come in pairs like <b> and </b> The first tag in a pair is the start tag, the second tag is the end tag The text between the start and end tags is the element content HTML tags are not case sensitive, <b> means the same as <B>

Structure of an HTML document HTML documents are enclosed with in <HTML> </HTML> tags Document Header Information placed in the header are essential to the inner working of the document but has nothing to do with the content of document Header is enclosed between <HEAD> and </HEAD> Typically the document title is placed with in the header <TITLE>document title</TITLE> Document Body Placed between <BODY> and </BODY> The purpose of the <body> tag is to define the HTML element that contains the body of the HTML document. Page defaults like Background color, Background, Text Color, Font Size, Font Weight etc can be specified as attributes <BODY BACKGROUND=“x.gif” TEXT=“Red”>this is my document body</BODY>

First Web Page Document Header Document body

Text Formatting – paragraphs and line breaks <P> for inserting a new paragraph <P>this is a new paragraph <BR> for line break Line breaks create forced hard returns in your document. There is a hard return below this text <br /> There is a hard return above this text Note: Use the <br> tag to insert line breaks, not to create paragraphs. Preserving White Space <pre> </pre>Demo Example <p><pre> Mary had a little lamb </pre></p>

Linking Hyperlink References other sources such as XHTML documents and images Both text and images can act as hyperlinks Created using the a (anchor) element Attribute href Specifies the location of a linked resource Link to e-mail addresses using mailto: URL

Linking the documents HTML allows linking of one HTML document to other as well as images Use ALINK, VLINK attributes (deprecated) of BODY tag to control the color of the hyperlink that is displayed Links are created in a web page through <A> tag (anchor tag) Syntax - <a href="url">Text to be displayed</a> Eg: <A HREF=http://www.google.com>This is a search engine</A>

First CGI Program This line The line Content-type:text/html\r\n\r\n is part of HTTP header is sent back to the browser and specify the content type to be displayed on the browser screen.

xampp an introduction The xampp server will start, minimize the window XAMPP is a free and open source cross-platform web server package, consisting mainly of the Apache HTTP Server, MySQL database, PHP Perl programming languages. XAMPP is available for Microsoft Windows, Linux, Solaris, and Mac OS X, and is mainly used for web development projects. Download Xampp software and install in /opt folder http://www.apachefriends.org/en/xampp-linux.html Starting of xampp software (You require a root permission) Open a terminal #cd /opt/lampp/ ~/opt/lampp #./lampp start The xampp server will start, minimize the window Check for server running

Open any browser and type http://localhost You will get xampp welcome screen as Once the server is started you can deploy your html and cgi files

How to execute cgi python files Type your first cgi program hello.py and keep it in the folder /opt/lampp/cgi-bin Change mode of file using chmod 755 hello.py UNIX command to make file executable. This hello.py script is a simple Python script which is writing its output on STDOUT file ie. Screen. Open the browser and type http://localhost/cgi-bin/hello.py You will get the following output on browser

GET and POST Methods Most frequently browser uses two methods two pass this information to web server. These methods are GET method and POST method The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows: 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:box. Never use GET method if you have password or other sensitive information to pass to the server. The GET method has size limitation: only 1024 characters can be sent in a request string. The GET method sends information using QUERY_STRING header and will be accessible in your CGI Program through QUERY_STRING environment variable.

Passing Information using POST method A generally more reliable method of passing information to a CGI program is the POST method. This packages the information in exactly the same way as GET methods, but instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes into the CGI script in the form of the standard input.

Simple FORM Example: GET Method The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows: hello.py: save this file in /opt/lampp/htdocs “post”

hello_get.py (save this file /opt/lampp/cgi-bin) cgitb is a library that dumps errors to the output – you don’t have to go searching for it in your log file. cgi.FieldStorage() is a pseudo-dictionary containing all the POST and GET variables and their values.

Passing Checkbox Data to CGI Program Checkboxes are used when more than one option is required to be selected

Passing Radio Button Data to CGI Program Radio Buttons are used when only one option is required to be selected.

Passing Text Area Data to CGI Program TEXTAREA element is used when multiline text has to be passed to the CGI Program.

Passing Drop Down Box Data to CGI Program Drop Down Box is used when we have many options available but only one or two will be selected.

Using Cookies in CGI Cookie  is a small piece of data sent from a website and stored in a user's web browser while a user is browsing a website. Cookies use a plain text format HTTP protocol is a stateless protocol. But for a commercial website it is required to maintain session information among different pages. For example one user registration ends after completing many pages. But how to maintain user's session information across all the web pages. 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.

Setting a cookie This is very easy to send cookies to browser. These cookies will be sent along with HTTP Header before to Content type filed. Assuming you want to set UserID and Password as cookies. So cookies setting will be done as follows

Retrieving a cookie

Exercise Create a student registration form using all html tags and display the information using python CGI program Program to accept the User Name and display a greeting message. Program to accept the User Name, Gender and Age and display a greeting message that is age appropriate. Eg: 60 year old Gentleman named Francis, display “Namaste Uncle Francis” 60 year old Lady named Benazir, display “Namaste Aunty Benazir” 18 year old guy named Mahesh, display “Hi! Mahesh. How are you dude? “