See All Titles |
![]() ![]() Configuring Web Servers for Python/CGI ScriptsThe next topics show you how to configure the most used Web servers in the market. Mostly you will see how to handle Python CGI scripts within Apache and Microsoft IIS Web servers. Python in ApacheFirst, let's see how Apache handles requests. When a file is called, Apache executes an action, which internally is known as handler. These handlers are usually implicitly related to the files, based on the file type. However, new Apache releases are able to assign handles to filename extensions or file locations, instead of only work with the file type. Python script files are handled in exactly the same way as other CGI scripts. Once a request is received, Apache calls the Python interpreter asking it to run the specific script. Depending on the Apache configuration, there are several actions to be performed when receiving a request (for instance, user authentication and file transfer). Apache comes with a predefined set of handlers for basic routine tasks. However, there are several third-party handler applications that can be very useful as well, such as the mod_python and mod_pyapache modules. Using these modules is not strictly necessary, but it reduces the overhead of your server and increases the speed of your application. Both of these reasons occur because the Python interpreter is not called for every single connection anymore. You can create Apache Handlers by building them into the Web Server, adding them to the Action directive, or implementing a module. The Apache official Web site is as follows: Configuring Apache for PythonThe following guidelines will help you configure your Apache installation to run Python in both Windows and UNIX systems. Steps 1–8 are specific for Win32 configurations.
At this time, you should be ready to launch your Web browser and to access your CGI script by typing its URL. For UNIX, if Apache and Python are set up correctly, all you need to do is place the Python scripts in the cgi-bin directory and set their permissions correctly. More information about this topic can be found at the newsgroup for discussions about running Apache under Windows at comp.infosystems.www.servers.ms-windows. mod_pythonmod_python is a module created by Gregory Trubetskoy that embeds the Python language interpreter within the Apache server, allowing Apache handlers to be written in Python. It provides nearly every possible handler to Apache. mod_python brings a considerable boost in performance over the traditional CGI approach, and adds flexibility in designing Web-based applications. In order to run it, you must have at least Python 1.5.2 and Apache 1.3. mod_python handlers by default do not perform any function, unless specifically told so by a configuration directive. These directives begin with Python, end with Handler (for example, PythonAuthenHandler), and associate a handler with a Python function. Therefore, the main function of mod_python is to act as a dispatcher between Apache handlers and python functions written by developers. The most commonly used one is PythonHandler. It is for a handler that has no specialized purpose, such as authentication. The default Apache action for this handler would be to read the file and send it to the client. Most applications you write will use this one handler. For more information, check out these sites:
mod_pyapacheThis module will speed up the execution of your CGI scripts written in the Python Language. It handles CGI scripts faster than other normal CGI scripts because the server embeds the Python Interpreter. Therefore, the performance penalty of executing an external one is eliminated. This module has the advantage of being CGI compatible—it works well when CGI scripts are simple and trusted and it provides total CGI control to your Python application. However, this module currently has some limitations, including the fact that it doesn't avoid database connections delay. Check out the following Web site for more information: http://www.msg.com.mx/pyapache/ You will find the latest version of the module in the ftp://www.bel-epa.com/pub/misc/ directory, where you will see a gzipped tar file named something like PyApache-x.yy.tar.gz. AOLserver Web ServerThis is a Web Server created and used by AOL. Note that anyone using AOLserver would be better off learning TCL. For details, see The project that embeds Python in the AOLServer Web Server, is now semi-stable for simple CGI-style operations, and provides a 4-5x speedup over the straight CGI. Check it out at http://pywx.sourceforge.net. Microsoft IIS and PWSYou can set up both Microsoft IIS Server and Personal Web Server (PWS) to call the Python interpreter to handle Python CGI scripts. Tip
PWS is Microsoft's free basic Web server for the Windows 95 platform. You need to pay close attention when using the PWS server because a version of PWS is part of the Front Page Personal Web Server, which doesn't run files from executable directories. Instead, it returns an error message. If you slide your mouse over the PWS icon in the taskbar, and it says Personal Web Server, you have the proper version. Now, let's demonstrate how to configure IIS and PWS for Python/CGI scripting. I assume that you have already installed Python on your system. On the Microsoft IIS server or on the Win95 MS Personal Web Server, you need to set up Python in the same way that you would set up any other scripting engine:
Now, you are ready to call your scripts. Make sure that they are stored in an executable directory in the Web server. The -u flag specifies unbuffered and binary mode for stdin, which is needed when working with binary data. This flag prevents cr-nl from being converted to newline combinations. Most developers agree that exposing the language behind your script works similar to saying "Welcome" to crackers around the world. Therefore, it is suggested to hide these details by using another extension, for example, .cgi, for your CGI scripts. You don't need to change the extension of all your files, just the ones that will be exposed by your site's Web interface. The other modules can continue to have the .py extension. The line in the registry would resemble the following: .cgi :REG_SZ: c:\ path\ to\ python.exe -u %s %s Note
Of course, this is no substitute for actually making sure that your scripts are secure. After restarting your computer, everything gets set up, and every script (with the proper extension) located at an executable directory is sent to the Python interpreter.
|
© 2002, O'Reilly & Associates, Inc. |