How to write code in CGI programing
What is CGI?
- 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.
- The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows:
- The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers.
- The current version is CGI/1.1 and CGI/1.2 is under progress.
Web Browsing
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 demands for the URL i.e., filename.
- Web Server will parse the URL and will look for the filename in if it finds that file then sends it back to the browser, otherwise sends an error message indicating that you have requested a wrong file.
- 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 and the programs are called CGI scripts. These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program, etc.
CGI Architecture Diagram
Web Server Support & Configuration
Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is configured to handle CGI Programs. All the CGI Programs to be executed by the HTTP server are kept in a pre-configured directory. This directory is called CGI Directory and by convention it is named as /var/www/cgi-bin. By convention, CGI files will have extension as .cgi, but you can keep your files with python extension .py as well.
By default, the Linux server is configured to run only the scripts in the cgi-bin directory in /var/www. If you want to specify any other directory to run your CGI scripts, comment the following lines in the httpd.conf file:
Here, I assumed that you have Web Server up and running successfully and you are able to run any other CGI program like Perl or Shell, etc.AllowOverride None Options ExecCGI Order allow,deny Allow from all Options All
First CGI Program
Here is a simple link, which is linked to a CGI script called hello.py. This file is being kept in /var/www/cgi-bin directory and it has following content. Before running your CGI program, make sure you have change mode of file using chmod 755 hello.py UNIX command to make file executable.#!/usr/bin/python print "Content-type:text/html\r\n\r\n" print '' print '' print 'If you click hello.py, then this produces the following output:Hello Word - First CGI Program ' print '' print '' print 'Hello Word! This is my first CGI program
' print '' print ''
Hello Word! This is my first CGI programThis hello.py script is a simple Python script, which is writing its output on STDOUT file i.e., screen. There is one important and extra feature available which is first line to be printed Content-type:text/html\r\n\r\n. This line is sent back to the browser and specifiy the content type to be displayed on the browser screen. Now, you must have understood basic concept of CGI and you can write many complicated CGI programs using Python. This script can interact with any other external system also to exchange information such as RDBMS.
HTTP Header
The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to understand the content. All the HTTP header will be in the following form:HTTP Field Name: Field Content For Example Content-type: text/html\r\n\r\nThere are few other important HTTP headers, which you will use frequently in your CGI Programming. Header |Description ___________________ Content-type: |A MIME string defining the format of the file being returned. Example is Content-type:text/html _________________ Expires: Date |The date the information becomes invalid. This should be used by the browser to decide when a page needs to be refreshed. A valid date string should be in the format 01 Jan 1998 12:00:00 GMT. _____________________ Location: URL |The URL that should be returned instead of the URL requested. You can use this field to redirect a request to any file. _______________ Last-modified: Date |The date of last modification of the resource. _______________ Content-length: N |The length, in bytes, of the data being returned. The browser uses this value to report the estimated download time for a file. _______________ Set-Cookie: String |Set the cookie passed through the string
CGI Environment Variables
All the CGI program will have access to the following environment variables. These variables play an important role while writing any CGI program.| Variable Name | Description |
|---|---|
| CONTENT_TYPE | The data type of the content. Used when the client is sending attached content to the server. For example, file upload, etc. |
| CONTENT_LENGTH | The length of the query information. It's available only for POST requests. |
| HTTP_COOKIE | Returns the set cookies in the form of key & value pair. |
| HTTP_USER_AGENT | The User-Agent request-header field contains information about the user agent originating the request. Its name of the web browser. |
| PATH_INFO | The path for the CGI script. |
| QUERY_STRING | The URL-encoded information that is sent with GET method request. |
| REMOTE_ADDR | The IP address of the remote host making the request. This can be useful for logging or for authentication purpose. |
| REMOTE_HOST | The fully qualified name of the host making the request. If this information is not available then REMOTE_ADDR can be used to get IR address. |
| REQUEST_METHOD | The method used to make the request. The most common methods are GET and POST. |
| SCRIPT_FILENAME | The full path to the CGI script. |
| SCRIPT_NAME | The name of the CGI script. |
| SERVER_NAME | The server's hostname or IP Address |
| SERVER_SOFTWARE | The name and version of the software the server is running. |
#!/usr/bin/python import os print "Content-type: text/html\r\n\r\n"; print "Environment<\br>"; for param in os.environ.keys(): print "%20s: %s<\br>" % (param, os.environ[param]) \br>\br>