Python CGI Examples

Python CGI Examples thumbnail
Python CGI applications power many web applications.

Certain applications that run on a Web server retrieve and process data from a user, then generate and transfer data back to the user's Web browser. These applications rely on CGI, or Common-Gateway-Interface, protocols to do their behind-the-scenes work. CGI is a World Wide Web standard that defines how server applications can interface with the Web server. Python is a general-purpose, scripting language that can be used to program CGI applications.

  1. Python CGI Support

    • The Python programming language includes a "cgi" library module that makes it possible to extract information from HTML -- HyperText Markup Language -- forms, parse and generate new HTML code and present this code to the web server. Python's CGI support is so complete, you can create your own simple Web server with just seven lines of Python code. The following code from the Python.org website implements a very basic Web server in Python.

      import SimpleHTTPServer
      import SocketServer
      PORT = 8000
      Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
      httpd = SocketServer.TCPServer(("", PORT), Handler)
      print "serving at port", PORT
      httpd.serve_forever()

    HTML Form Processing

    • Python's “cgi” module enables you to extract data sent from a Web form sent using the "GET" or "POST" method. This module contains the method "cgi.FieldStorage()," which extracts all the information sent by the form. You can then search this data for information or you can directly access the value assigned to a specific field by name. For example, Python code can get the data from the form fields named "firstname" and "lastname" and return a personalized message to the user's browser. The "Python.org" website features many successful applications that rely upon Python. The "Journyx Timesheet" is an example of an online application that retrieves and processes complex Web form data.

    Outputting HTML Code

    • Python CGI code can be used to retrieve static web pages stored as files on a server and pass them along to the Web server for broadcast. You can also generate new, dynamic web pages. For example, a Python script can read the browser cookies stored on your computer and generate a Web page that appears to have been created especially for you -- with your name, the last page you visited, a list of items you last purchased from the site or items you might like. Python can be used to create entire websites, blogs or maintain an organization's internal documents. Python.org mentions the EZRO Content Management System as an example of an Python-based application that is used to create, distribute and maintain documents on local or wide area networks.

    Tapping The Database

    • With the addition of a free, open-source module called MySQLdb, you can create Python CGI scripts that access and manipulate databases. The objects and methods in the MySQLdb module give Python programmers the hooks needed to login, query, create, read and write data to MYSQL databases. As an easy to use, general purpose language, Python extends the power of the database software. The Python.org site lists the Web company Gusto as an example of an online company that successfully uses Python's data-processing capability. Gusto relies upon Python to mine for information in its databases that can help the company better serve the members of its travel-service social network.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured