< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162148047091217051046165216

Configuring Web Servers for Python/CGI Scripts

The 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 Apache

First, 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:

http://www.apache.org/

Configuring Apache for Python

The 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.

Step 1.

Installing Python in the C:\ Python directory is a more convenient way to handle environment paths.

Step 2.

It is convenient if you have your CGI files in the same drive as the WINNT system files.

Step 3.

Verify if you have a system variable called PATH that contains the Python interpreter's (python.exe) directory (if necessary, create it).

Step 4.

Create a system variable called PYTHONPATH. It must contain the list of directories to be used when searching for Python files.

Step 5.

Use ASSOC to setup a file extension for Python.

									
ASSOC .py=PythonScript

								
Step 6.

Use FTYPE to associate the previous setting to the Python executable.

									
FTYPE PythonScript=python.exe %1 %*

								
Step 7.

Add the extension .py to the system environment variable PATHEXT. This variable stores the list of executable extensions (for example, PATHEXT=.EXE;.COM;.BAT;.CMD;.py).

Step 8.

Install Apache on your system's root drive, that is, "c:\ Apache". Installing Apache in this directory helps you during the whole configuration process.

Step 9. Edit your C:\WINNT\system32\drivers\etc\hosts ile, adding the IP address of your machine. This file is the NT equivalent to UNIX /etc/hosts table file.

The following steps tell you how to configure the Apache Web Server. Note that nowadays, the whole Apache configuration can be set using one unique file: httpd.conf.

Step 10.

In the access.conf file, make the following changes:

									
<Directory /apache/htdocs>
Options Indexes ExecCGI for Python/CGI scripts>

								
Step 11.

In the httpd.conf file, make the following changes:

									
ServerRoot /apache

								
Step 12.

In the srm.conf file, make the following changes. You also have the option to set PYTHONPATH here using the command SetEnv, instead of defining it as a system environment variable. Note that there are two AddHandler settings. The former identifies the extension to be associated with CGI scripts, and the latter allows you to use the .cgi extension in your files, in order to hide from crackers, the language used to implement your site. Of utmost importance is to make certain that you're using Python in unbuffered mode (SetEnv PYTHONUNBUFFERED 1) and to set (or pass) PYTHONPATH as a system environment variable. Forgetting to set either of these parameters is the most common reason for "premature end of header" errors.

									
DocumentRoot /apache/htdocs
ScriptAlias /cgi-bin/ /apache/cgi-bin/
PassEnv PYTHONPATH
SetEnv PYTHONUNBUFFERED 1
AddHandler cgi-script .py
AddHandler cgi-script .cgi

								
Step 13.

Place your scripts in your cgi-bin directory.

Step 14.

If you are using an UNIX system, make sure that the first line of your script contain a shebang to identify the location of the Python interpreter.

Step 15. Optionally, you can configure the server to run scripts only from the cgi-bin directory by replacing the following line in the access.conf file:
									
<Directory /path/to/your/httpd/cgi-bin> Options Indexes FollowSymLinks
 </Directory>

								
with
									
<Directory /path/to/your/httpd/cgi-bin> Options FollowSymLinks ExecCGI
 </Directory>

								

If you want to run your scripts from any directory, comment the previous setting and add the following one:

									
<Directory /path/to/your/httpd/htdocs> Options All </Directory>

								
Step 16.

Set the read and execute permissions of your script. If you are using an UNIX system, you should type chmod 755 yourscript.py.

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_python

mod_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_python Web site

http://www.modpython.org/

mod_python installation procedures

http://www.modpython.org/live/mod_python-2.4/doc/installation.html

mod_pyapache

This 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 Server

This is a Web Server created and used by AOL. Note that anyone using AOLserver would be better off learning TCL. For details, see

http://www.aolserver.com

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 PWS

You 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:

  1. Run REGEDIT.EXE

  2. Find the following key:

    								
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\
    ScriptMap
    
    							
  3. Once there, select the menu selection EDIT, New, String Value, and enter the following line (using the correct path):

    								
    .py :REG_SZ: c:\ path\ to\ python.exe -u %s %s
    
    							

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.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

[nd]u flag
AOLserver Web server
      configuring Web servers for Python/CGI scripts
Apache
      configuring Web servers for Python/CGI scripts 2nd 3rd 4th 5th
applications
     AOLserver Web server
            configuring Web servers for Python/CGI scripts
     Apache
            configuring Web servers for Python/CGI scripts 2nd 3rd 4th 5th
      Front Page Personal Web Server
     Microsoft IIS Server
            configuring Web servers for Python/CGI scripts 2nd
     Personal Web Server (PWS)
            configuring Web servers for Python/CGI scripts 2nd
CGI scripts
      configuring servers for 2nd 3rd 4th 5th 6th 7th
configuring
      servers for Python/CGI scripts 2nd 3rd 4th 5th 6th 7th
development
     Web
            configuring servers for Python/CGI scripts 2nd 3rd 4th 5th 6th 7th
flags
      [nd]u
Front Page Personal Web Server
functions
      PythonHandler
handlers, Apache 2nd
Internet
     development for
            configuring servers for Python/CGI scripts 2nd 3rd 4th 5th 6th 7th
Microsoft IIS Server
      configuring Web servers for Python/CGI scripts 2nd
mod_pyapache module
mod_python module
modules
      mod_pyapache
      mod_python
Personal Web Server (PWS)
      configuring Web servers for Python/CGI scripts 2nd
programs
     AOLserver Web server
            configuring Web servers for Python/CGI scripts
     Apache
            configuring Web servers for Python/CGI scripts 2nd 3rd 4th 5th
      Front Page Personal Web Server
     Microsoft IIS Server
            configuring Web servers for Python/CGI scripts 2nd
     Personal Web Server (PWS)
            configuring Web servers for Python/CGI scripts 2nd
PWS (Personal Web Server) 2nd
PythonHandler function
scripts
     CGI
            configuring servers for 2nd 3rd 4th 5th 6th 7th
servers
      configuring for Python/CGI scripts 2nd 3rd 4th 5th 6th 7th
software
     AOLserver Web server
            configuring Web servers for Python/CGI scripts
     Apache
            configuring Web servers for Python/CGI scripts 2nd 3rd 4th 5th
      Front Page Personal Web Server
     Microsoft IIS Server
            configuring Web servers for Python/CGI scripts 2nd
     Personal Web Server (PWS)
            configuring Web servers for Python/CGI scripts 2nd
Trubetskoy, Gregory
World Wide Web
     development for
            configuring servers for Python/CGI scripts 2nd 3rd 4th 5th 6th 7th

© 2002, O'Reilly & Associates, Inc.