Table of Contents

Web - CGI (Common Gateway Interface)

About

Common Gateway Interface (CGI) is the oldest interface, and is supported by nearly every web server out of the box.

The web server will:

What is a cgi-compatible executable?

The executable is called a cgi-compatible executable if it:

Any script (written in php, python, bash, ..) can be an a cgi-compatible executable.

Example

Php

This section shows how to use Php as a CGI script.

The web server would be configured to call the php-cgi executable.

For instance, you could:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>

Python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# cgitb enable debugging
# nice call trace vs “Internal Server Error”  
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!"

Specification

URL / URI

The URL takes this specific form

<scheme>://<host>:<port>/<path>?<query>

where:

Request

Environment variables 1) are used to pass data about the http request from the server to the script.

Response

A script will return the response via the standard output file descriptor 2)

The output may have one of the following format:

Performance

Programs using CGI to communicate with their web server need to be started by the server for every request. So, every request starts a new process (for python, a new Python interpreter) – which takes some time to start up – thus making the whole interface only usable for low load situations.

FastCGI tries to solve this shortcoming by loading the code into memory and making the script wait for a second request, making the process faster.

Documentation / Reference