The CGI (Common Gateway Interface) protocol is a standard way for external programs and application environments to interact with Web servers. While a plain HTML file on the Web is static, a CGI program (or script) is executed at the time that the request is made from the user, and the content is created on the fly. To see how this works, let's create a simple script in Python and have it executed by the Apache Web server.
1. Create a new file using the IDLE editor or your favorite text editor.
2. Place the following code into the new file and save this file as chl3_l .py:
#!c:/Python2 5/python from os import * from cgi import *
print "Content-type: text/html\n\n" print "<BR><B>Hello Python!</B>"
3. Save the file to the cgi-bin directory under the root of your Apache installation. For example, if the Apache system was installed to c: \Program Files\Apache Software Foundation\Apache 2.2, then save the file into:
C:\Program Files\Apache Software Foundation\Apache 2.2\cgi-bin
For this example, the file is called chl3_l.py, to indicate that it is the first program you created in this chapter, but you can call it whatever you like. Whatever you decide to name the file, remember that name for the next step.
4. Launch a Web browser and enter the following URL into the navigation box: http:// localhost/cgi-bin/ch13_1.py
Obviously, if you have used a different name for the file, replace the chl3_l in the above URL with your own name. In any case, the file must end with the .py extension and must be in the cgi-bin directory for this to work.
You should see a display similar to the one shown in Figure 13.2. Note that while this particular display is in the Firefox (Mozilla) Web browser, the resulting display would be the same in any Web browser. The output of your little program is simple HTML and will look the same no matter what you view it in:
Was this article helpful?