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:

Example

Php

Php is a CGI script.

For instance, you could:

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

Python

  • You may need to save this code with a .py or .cgi extension.
  • Additionally, this file may also need to be in a cgi-bin folder, for security reasons.
#!/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 where the path is the path to the script

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

Request

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

  • AUTH_TYPE
  • CONTENT_LENGTH
  • CONTENT_TYPE
  • GATEWAY_INTERFACE
  • HTTP_*
  • PATH_INFO
  • PATH_TRANSLATED
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_HOST
  • REMOTE_IDENT
  • REMOTE_USER
  • REQUEST_METHOD
  • SCRIPT_NAME
  • SERVER_NAME
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_SOFTWARE

Response

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

The output may have one of the following format:

  • Non-parsed Header: The script returns a original HTTP response message as specified by the HTTP specification.
  • Parsed Header: The script returns a CGI response message. 3)

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





Discover More
FastCGI

is an open extension to CGI. is a replacement for “standard” CGI scripts. In old-school CGI, the web server would start up your script, send it a request, wait for a response, then the script would...
Web - Server

A web server is a HTTP server that respond to HTTP request, generally returning a web page (HTML) (but it can serve any type of files). The request is handled: by native handlers (module) (if the server...



Share this page:
Follow us:
Task Runner