See All Titles |
![]() ![]() An Introduction to CGICGI (Common Gateway Interface) is a standardized way for the Web Server to invoke an external program to handle the client request. It is possible for the external program to access databases, documents, and other programs as part of the request, as well, and present customized data to viewers via the Web. A CGI script can be written in any language, but here, of course, we are using only Python. CGI enables you to handle from the low end of mail-forms and counter programs to the most complex database scripts that generate entire Web sites on-the-fly. CGI's job is to manage the communication between browsers and server-side scripts. Programs that implement CGI routines are called CGI programs or CGI scripts. These scripts are usually visualized, through the Web browser, in a directory called /cgi-bin, but their actual location in the file system varies. You have two ways to pass the information from the browser to the CGI script: You can use either the POST or the GET method on your HTML Form. The POST method uses the standard input to transfer the information, whereas the GET method places the information into an environment variable. The GET method has the limitation of the size of the environment variable and the advantage of making it possible to encapsulate an HTML Form within an URL. Another downside to the GET method is that it might leak information. If there is an external image (for instance, a banner ad) or an off site link the user clicks on the page generated by the CGI script, the form results will be passed to that third party through the referer header. Therefore, don't use banner ads or off-site links for the CGI script handling a GET form. The POST method, in theory, has no limits to the amount of information that can be passed to the server. The disadvantage is that you can't send the information as part of the URL. You must have a form in your page. Python uses the cgi module to implement CGI scripts and to process form handling in Web applications that are invoked by an HTTP server. The cgi module also hides the differences between GET and POST style forms. Here is a very simple script to start you out with Python CGI processing: 1: #!/usr/bin/python 2: print "Content-Type: text/plain\n\n" 3: print "Hello Python World!" Line 1: Path to the Python interpreter (UNIX only). Line 2: Pass the MIME type to the browser in order to let it know how to render the information. Line 3: Prints a string in the browser window. In order to execute it, place it on a executable directory on your Web server and call it from your Web browser. If you are working on a UNIX-like OS, you need to run chmod a+x scriptname. Sometimes, CGI implementations also cause slow response times in the system. Keep in mind that each CGI invocation creates a new process, starts a new instance of the Python interpreter, and imports all the necessary library modules. Okay, I suppose you got the picture. The goal here is to let you know that sometimes the problem is not in the code, but in the infrastructure that surrounds it. Within your CGI script, you should consider avoiding using fork() as much as you can. But fork() is not the slow(est) part—it is the interpreter startup time and database connection setup. To get help with that, try using mod_pyapache or mod_python. The following links take you to sites that demonstrate and clarify the use of CGI routines:
|
Index terms contained in this sectionbrowserspassing data to CGI scripts from CGI (Common Gateway Interface) scripts 2nd 3rd cgi module Common Gateway Interface (CGI) scripts 2nd 3rd data passing from browsers to CGI scripts executing CGI scripts fork() method GET method 2nd methods fork() GET 2nd POST 2nd mod_pyapache module mod_python module modules cgi mod_pyapache mod_python passing data from browsers to CGI scripts POST method 2nd processing CGI scripts scripts Common Gateway Interface (CGI) 2nd 3rd transferring data from browsers to CGI scripts |
© 2002, O'Reilly & Associates, Inc. |