Skip to main content

Python CGI Programming

Python CGI Programming

Until now we did a lot of stuffs with programming that are not related to web or network. Now it’s time for CGI. As the name suggests, CGI means “Common” gateway interface for everything. CGI is one of the important parts of HTTP (Hyper-Text Transfer Protocol).
It is a set of standards that defines a standard way of passing information or web-user request to an application program & to get data back to forward it to users. This is the exchange of information between web-server & a custom script. When the users requested the web-page the server sends the requested web-page. The web server usually pass the information to all application programs that processes data and sends back an acknowledged message; and this technique of passing data back-&-forth between server & application is the Common Gateway Interface. The current version of CGI is CGI/1.1 & CGI/1.2 is under process.

Browsing

What happens when a user clicks a hyperlink to browse a particular web-page or URL (Uniform Resource Locator).
The steps are:
  • Browser contacts the HTTP web server for demanding the URL
  • Parsing the URL
  • Look for the filename
  • If it finds that file, a request is send back
  • Web browser takes a response from web server
  • As the server response, it either shows the received file or an error message.
It may become possible to set-up an HTTP server because when a certain directory is requested that file is not sent back; instead it is executed as a program and that program’s output is displayed back to your browser.

Configuring CGI

The steps are:
  1. Find out which user is running the Web-server
  2. Check for server configuration to see if you can run the scripts in a particular directory
  3. Check for file’s permission
  4. Make a clear assurance that scripts you made are readable and executable by the web-server user
  5. Make sure the Python-Script’s first line refers to web-server that the interpreter can run
The architecture of CHI is shown below:
architecture of CGI

Python CGI Program Structure

The output of Python CGI script must consist of two sections separated by a blank line. The first part contains the number of headers that describes the client what kind of data is following.
Python code header section looks something like this:
Example:
#!/usr/bin/python
print "Content-Type : text/html"

# then comes the rest hyper-text documents
print "<html>"
print "<head>"
print "<title>My First CGI-Program </title>"
print "<head>"
print "<body>"
print "<h3>This is HTML's Body section </h3>"
print "</body>"
print '</html>'
Save this file as CGI.py. When you will open that saved file, the output becomes:
Output:
This is HTML's Body section
This is a simple Python script that writes its output on STDOUT file i.e. on screen.

Use Of CGI Module

We programmers write CGI scripts in Python, they can add these lines:
import cgitb
cgitb.enable()
The above code triggers special exception handler that will display detailed report in the web-browser in case of occurrence of any error.

HTTP Header

Few are the important lists of HTTP header frequently used in CGI programs. These are:
HTTP headerValue
Content-typetext/html
ExpiresDate
LocationURL
Set-CookieString
Content-lengthN

CGI Environment Variables

Environment VariablesDescription
CONTENT_TYPEdescribes the data-type of the content
HTTP_COOKIEreturns the visitor’s cookie, if one is set
CONTENT_LENGTHIt is available for POST request to define the length of query information
HTTP_USER_AGENTdefines the browser type of the visitor
PATH_INFOdefines the path of CGI script
REMOTE_HOSTdefines the host-name of the visitor
REMOTE_ADDRdefines the IP Address of the visitor
REQUEST_METHODused to make request & the most common methods are – GET and POST

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

Java Method Overriding

Java Method Overriding Declaring a method in the subclass which already exists there in the parent class is known as method overriding. When a class is inheriting a method from a superclass of its own, then there is an option of overriding the method provided it is not declared as final. The advantage of using overriding is the ability to classify a behavior that’s specific to the child class and the child class can implement a parent class method based on its necessity. There are certain rules that a programmer should follow in order to implement overriding. These are: In Java, a method can only be written in the child class and not in same class. Argument list should be exactly the same as that of the overridden method of that class. Instance methods can also be overridden if they are inherited by the child class. A constructor cannot be overridden. Final – declared methods cannot be overridden. Any method that is static cannot be used to override. The return type must have to be the...