Web - CGI (Common Gateway Interface)

> (World Wide) Web - (W3|WWW)

1 - About

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

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.

Writing CGI programs, while still possible, is no longer recommended.

The upside of CGI is that it is simple, one file, one request, one response.

3 - Example

  • 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.
cgi-bin/mycgi.py
#!/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!"
Advertising

4 - Documentation / Reference