Configuring Web Servers for Python/CGI Scripts - Python

The next topics show you how to configure the most used Web servers in the market. Mostly you will see how tohandle 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 tofilename 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, Apachecalls the Python interpreter asking it to run the specific script. Depending on the Apache configuration, there areseveral 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 theAction directive, or implementing a module.

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 PYTHON PATH. It must contain the list of directories to be used whensearching 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 .pyto 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 helpsyou 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 now a days, the whole Apache configuration can be set using one unique file: httpd .conf.

Step 10: In the access.conffile, make the following changes:

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

Step 11: In the httpd.conffile, make the following changes:

ServerRoot /apache

Step 12: In the srm .conffile, make the following changes. You also have the option to set PYTHON PATH hereusing the command Set Env, instead of defining it as a system environment variable. Note that there aretwo Add Handler settings. The former identifies the extension to be associated with CGI scripts, and the latter allows you to use the .cgiextension in your files, in order to hide from crackers, the language usedto implement your site.

Of utmost importance is to make certain that you're using Python in unbuffered mode (Set Env PYTHON UNBUFFERED 1) and to set (or pass) PYTHON PATH 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
AddHandlercgi-script .py
AddHandlercgi-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 identifythe location of the Python interpreter.

Step 15:Optionally, you can configure the server to run scripts only from the cgi-bin directoryby replacing the following line in the access.conffile:

<Directory /path/to/your/httpd/cgi-bin> Options Indexes FollowSymLinks </Directory>
with
<Directory /path/to/your/httpd/cgi-bin> Options FollowSymLinksExecCGI </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 your script .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 cgibin directory and set their permissions correctly.

More information about this topic can be found at the news group for discussions about running Apache unde rWindows at comp .info systems .www .servers .ms -windows.

mod_python

mod _pythonis a module created by Gregory Trubetskoy that embeds the Python language interpreter within theApache server, allowing Apache handlers to be written in Python. It provides nearly every possible handler toApache.

mod _pythonbrings 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, Python Authen Handler), andassociate a handler with a Python function. Therefore, the main function of mod_pythonis to act as a dispatcherbetween Apache handlers and python functions written by developers.

The most commonly used one is Python Handler. 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.

mod_pyapache

This module will speed up the execution of your CGI scripts written in the Python Language. It handles CGIscripts 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 trustedand 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.

AOLserver Web Server

This is a Web Server created and used by AOL. Note that anyone using AOLserver would be better off learningTCL. The project that embeds Python in the AOLServer Web Server, is now semi-stable for simple CGI-styleoperations, and provides a 4-5x speedup over the straight CGI.

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 alreadyinstalled 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 waythat you would set up any other scripting engine:

  1. Run REGEDIT.EXE
  2. Find the following key:
  3. HKEY_LOCAL_MACHINESYSTEMCurrentControlSet ServicesW3SVCParametersScriptMap
  4. 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 Webserver. The -u flag specifies unbuffered and binary mode for stdin, which is needed when working with binary data.

This flag prevents cr-nlfrom 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 .pyextension. 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.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Python Topics