Developer: Open Source
   DOWNLOAD
Download Oracle Database XE
 
   TAGS
python, opensource, xe, All
 

 

Build a Rapid Web Development Environment for Python Server Pages and Oracle


by Przemyslaw Piotrowski

 

Get into high-level Web programming with the reliable Oracle Database 10g, scalable Apache Web server, and intuitive, object-oriented Python programming language.

Published July 2006

 

 

Developers today benefit from a range of server-side technologies that function as the middle layer between a Web server and the database, including JavaServer Pages (JSP), Active Server Pages (ASP), PHP, and if you're an Oracle developer, PL/SQL Server Pages.

When I was introduced to Python programming language back in 2003, I was amazed by its elegance and ease of use. It is so high level that it writes almost like plain English, looking like pseudocode to unaware programmers. The dynamic nature of Python lets you write a minimum of unambiguous code to accomplish even the most complex tasks.

In this article I will outline an easy method of creating a complete Python Server Pages/Oracle Database 10g rapid Web development environment. I will assume you have none of the components installed on your Windows or Linux system. (If you do, however, you will need to skip some steps accordingly.) All of the featured components are free to download and carry no licensing costs.

Python and Python Server Pages Background

Python offers strong introspection capabilities, dynamic typing, complete object-orientation, extensibility, and platform independence (Windows, Linux, Solaris, Mac OS, Nokia / S60, Pocket PC, and counting). It allows you to be highly productive and results-oriented, focusing on solving problems rather than coping with language-intrinsic features. Python is extremely readable and easy to maintain by putting certain requirements on the code style. I have many Python one-liners (sometimes two-liners if they include the import statement) that actually perform complex tasks, and these are not just "hello-worlders."

One of Python's greatest strengths is its extensive standard library, which provides an out-of-the-box set of modules for most programming tasks (Python programmers refer to it as the "batteries-included strategy") including file I/O, system interaction, networking, data parsing and manipulation, threading, profiling, unit-testing, introspection, XML processing, and many more.

You'll be amazed by the speed of development with Python--you can finally engage the computer to understand what you want to do, not the opposite. You can also use the built-in docstrings -- in-line help strings -- to enable other programmers quickly grasp the idea behind your class or function. (Creating a documentation from the source code is a snap.) For these reasons, Google and NASA are big Python shops, and Microsoft is developing its own version of Python on the .NET platform called IronPython.

Python Server Pages (PSP) are to Python what JavaServer Pages are to Java. Pages rendered with PSP can perform 50 times better than those served with regular CGI processing. Mod_python includes a range of specialized modules that make Web development much easier.

Solution Components

The complete Web development environment must consist of at least three essential components: the Web server, database, and server-side backend technology. There are also functional requirements like reliability, scalability, and interoperability. To address them I have chosen Apache HTTP Server 2.0, Oracle Database 10g Express Edition (XE), and Python 2.4 (with mod_python and cx_Oracle modules). (Read on to find the reasoning behind these choices.)

Windows users will certainly take advantage of "Click Next" installations of all these components, although Linux users still won't be required to type much. I've simplified the installation procedure to make use of the available binaries.

Oracle Database 10g Express Edition

In February 2006, Oracle announced the production release of Oracle Database XE. It is an entry-level, small-footprint version of Oracle Database 10g Release 2 and is completely free to develop, deploy, and distribute.

Available for Windows and Linux operating systems, Oracle Database XE is an excellent choice for PHP, Java, or .NET developers. Furthermore, with the great amount of available third-party modules and drivers, it can be accessed from a range of different programming languages, including Python.

Oracle Database XE comes in two flavors, Western European and Universal. The former includes support only for LATIN1 characters while the latter's default charset is AL32UTF8. The Universal version also includes administrative interfaces translated into Brazilian Portuguese, Chinese (Simplified and Traditional), English, French, German, Italian, Japanese, Korean, and Spanish. Since nobody wants to be explicitly limited to LATIN1, I recommend installing and using the Universal version.

Installing and Running Oracle Database XE

Download OracleXEUniv.exe for Windows or oracle-xe-univ-10.2.0.1-1.0.i386.rpm for RPM-based Linux distributions. (Oracle Database XE is available for Debian as well, but I will not cover it in this document.)

On Windows:

  1. Login to Windows using account with Administrator privileges.
  2. Remove the ORACLE_HOME variable from your system if it has been set up before.
  3. Double-click the downloaded installation executable from the step 1.
    Figure 1
  4. In the Oracle Database 10g Express Edition - Install Wizard , click Next.
  5. In the License Agreement window, select I accept and then click Next.
    Figure 2
  6. When the Choose Installation Location window pops up, you can either accept the default directory target or select the desired target location clicking the Browse button. After completing this step, click Next.
  7. If you are prompted for a port number, you should specify one. The default port numbers for Oracle Database XE are
    • 1521 for Oracle database listener
    • 2030 for Oracle Services for Microsoft Transaction Server
    • 8080 for XE administrative graphical interface accessible via Web browser
  8. Now you need to set password for SYS and SYSTEM database administrative accounts. In the Specify Database Passwords window, enter the desired password and confirm it below. Click Next.
  9. The Summary window offers an overview of the installation parameters you have set through the whole process. If you accept them, click Install to begin installation.
  10. Click Finish to complete the installation process. If you would like to launch the administrative browser interface, select the Launch the Database homepage option. You have successfully installed Oracle Database XE on Windows.

On Linux:
  1. Log in as root.
    Figure 3
  2. Change current path to the directory where you've downloaded the RPM installer from Step 1.
  3. Launch the installation process by running:
    # rpm -ivh oracle-xe-univ-10.2.0.1-1.0.i386.rpm
    
    (The installation process begins.)
  4. Now you need to set parameters for the installed database, when prompted run the following command:
  5. # /etc/init.d/oracle-xe configure
    
  6. Enter the following details:
    • HTTP port for the Oracle Database XE graphical user interface (default 8080)
    • Port for the Oracle database listener (default 1521)
    • Password for the SYS and SYSTEM administrative database accounts
    • Whether you want the database to start automatically when after system is rebooted.
  7. At any time you can reconfigure the database by running
    # /etc/init.d/oracle-xe configure
    
  8. To control the instance of the database by starting and stopping it, either use the menu options that were installed with XE or run from the command line:
    # /etc/init.d/oracle-xe start
    
    or
    # /etc/init.d/oracle-xe stop
    

You can now log in to XE's graphical administrative interface by opening http://127.0.0.1:8080/apex/ in your browser. (If the database was configured using custom parameters, change the path appropriately.)

Installing Apache HTTP Server 2.0

Apache HTTP Server is currently deployed on at least two-thirds world's Web servers, according to the latest Netcraft statistics. Enabling even the most scalable architectures, it is available for a choice of system platforms, including Microsoft Windows and Linux.

Apache is however much more than just a Web server. Apache Software Foundation provides support for many open-source, enterprise-grade products, including the famous Struts framework, Tomcat servlet container, Derby database, Axis platform, FOP processor, and Xerces parser, to name just a few. Apache HTTP Server is quite easy to configure and provides unmatched flexibility for various server-side configurations. It also serves as the heart of Oracle HTTP Server, as you may already know.

On Windows:
  1. Login as Administrator and download the MSI Installer apache_2.0.55-win32-x86-no_ssl.msi; double-click it to begin the installation process.
  2. Apache HTTP Server 2.0 - Installation Wizard will be shown. Click Next to continue.
    Figure 4
  3. In the License Agreement window select I accept the terms in the license agreement and click Next.
  4. You will find some Apache HTTP server information in the Read This First window, click Next to continue.
  5. The Server Information dialog will appear. At this point you need to enter the following information:
    • Network Domain - DNS domain in which your server currently is or will be registered in, for the full DNS name py.oracle.com you would need to enter just oracle.com.
    • Server Name - server's full DNS name without skipping anything like in the above Network Domain.
    • Administrator's Email Address - by default this email will be shown to clients along with error messages generated by the server.
    • to which account to install Apache serve—either for All Users or only for the Current User. (Since you don't want Apache to corrupt Oracle Database XE default HTTP 8080 port, I recommend selecting the first, more convenient option.)
      Figure 5
  6. After setting these parameters, click Next.
  7. In the next Setup Type window you will be given a choice to select Typical or Custom features. Unless you want to change the target location, use the Typical installation, which skips headers and libraries for compiling modules. (You'll be using pre-compiled modules in later steps.) From this point on I will refer to the Apache installation directory as APACHE_HOME. Click Next to continue.
  8. When the Ready to Install the Program window shows up, click Install to initiate the installation process.
  9. Click Finish to complete.

On Linux (RPM method):

  1. You can skip this section if your system already includes Apache HTTP server. Most Linux distributions install Apache by default, but be sure you have 2.0.x version installed. (Use rpm -qa | grep apache command to introspect.)
  2. Download httpd-2.0.55-1.i386.rpm.
  3. Run the following command to install:
    # rpm -ivh httpd-2.0.55-1.i386.rpm
    
    (Your default location for APACHE_HOME may vary depending on the --prefix parameter, it's usually /usr/local/apache2.)
  4. The installation from sources procedure is described at http://httpd.apache.org/docs/2.0/install.html; if your distribution doesn't handle RPMs, then it is the best guide to follow.

To check whether Apache HTTP server is working properly, go to http://127.0.0.1/ and see if the default welcome page shows up.

The configuration of Apache is incomplete, however—you still need to install Python and mod_python. Follow the steps below to complete the installation of the Python interpreter on your operating system.

Installing Python

On Windows:

  1. Go to http://www.python.org/ and in the Quick Links section locate the direct link to the MSI file named Windows Installer. (Python 2.4.3 at the time of writing). Save the python-2.4.3.msi file on your computer.
  2. Logged in as user with Administrative rights, double-click the downloaded installer executable. The screen shown below will appear when you will be given a choice to install Python for all or just currently logged user. After selecting the desired option, click Next.
    Figure 6
  3. In the Select Destination Directory window, leave the default path or change it to the desired target location and click Next.
  4. At this point you can enable or disable features to be installed on your system. I suggest leaving all those items in their default state, but if you decide not include some of them, feel free to do so. After selecting desired features, click Next to begin the installation process.
    Figure 7
  5. Click Finish to complete.

On Linux:

Linux installation of Python depends completely on the system distribution. There are wide installation choices and the chances that you are already running Python 2.4 are very high if your system is not that old. Visit http://www.python.org/download/linux/ to see the available choices.

Installing mod_python

We will connect Python to Apache HTTP Server using mod_python, module originally written by Gregory Trubetskoy and generously donated to the Apache Software Foundation. This module has been available on the Internet since 2000, and became part of Apache modules collection in 2002.

Mod_python gives you access to many low-level Apache internals and provides unrivalled flexibility compared to other server-side, Apache-based solutions (like mod_php or mod_perl). You may or may not use these internals, but since we'd like to focus on rapid web development, I will describe the high-level interface it provides, focusing precisely on PSP.

With mod_python you can take the full advantage of Python and its modules, writing server pages that can be cached and wrapped in reusable templates. Please note that the PSP engine is one of many interpreters mod_python provides, but since we would like to embed Python code directly in the page source, PSP is the preferred solution.

Installing mod_python

 

Troubleshooting Windows installation

Under certain system configurations Apache is unable to find paths to Python libraries and modules. An easy workaround is changing the account that Apache 2 service is running on.

Run services.msc command and from the list of available services select Apache 2; right-click and select Properties . On the Log On tab set the This account option and enter passwords for the current user. Press OK and restart the service.

On Windows:
  1. Download mod_python-3.2.8.win32-py2.4.exe here and double-click the downloaded executable.
  2. Click Next to continue.
  3. Mod_python installer will try to locate the default Python 2.4 interpreter. To accept the choice click Next again.
  4. To begin the installation process, press the Next button.
  5. Upon installation end, the installer will ask for Apache 2 location ( APACHE_HOME). Use the tree browser to navigate to the appropriate directory and press OK.
  6. Click Finish to complete installation.

On Linux:

  1. This step will require compilation of the module. For this purpose, download the mod_python-3.2.8.tgz file here and place it in /tmp directory of your Linux system.
  2. You will compile mod_python as Dynamic Shared Object (DSO) so that it is loaded by the server at run time. This lets you compile it without actually recompiling the Apache server. Ideally, the ./configure; make under /tmp directory should be enough to compile the module. Depending on the system configuration there might be situations where you need to manually specify Apache, Python and/or Flex locations. If you run across compilation errors consult the official mod_python installation guide.
  3. Finally you need to install the compiled module. You need to be root to do that. Still under /tmp directory run $ make install and wait until the execution finishes.

 

Configuring Apache HTTP Server to Handle PSP

After mod_python installation you must make Apache aware of it. For this purpose open APACHE_HOME/conf/httpd.conf for editing(for default Linux location of httpd.conf, consult your system documentation) and append the following lines at the end:

LoadModule python_module modules/mod_python.so
AddHandler mod_python .psp .psp_
PythonHandler mod_python.psp
PythonDebug On

The first line loads mod_python into Apache. The actual path of mod_python.so may vary but the installer should give you the proper path upon finishing. (On Linux most likely it would be libexec/mod_python.so.) The second line informs Apache which files should be handled by PythonHandler, specified on the third line (more about .psp_ usage in the section below). The last directive switches mod_python into debugging mode; when PythonDebug directive is set to On, it will dump traces for the first unhandled exception in the running application. Setting this flag is highly recommended for development purposes, but make sure you turn it off on production environments so as to not expose certain system internals to visitors.

If you would like mod_python pages to be used as default index pages, locate in httpd.conf the following line and change it from
DirectoryIndex index.html index.html.var
to
DirectoryIndex index.html index.psp index.html.var

Now restart Apache. On Windows you usually do that with the Apache Monitor in the system tray or by running net stop apache2 and then net start apache2; on Linux it's as simple as apachectl restart.

Using mod_python

PSP are plain text files that include Python code surrounded with <% and %> tags. Python expressions are enclosed within <%= and %> tags. Directives enable inclusion of other PSP pages; they are wrapped by <%@ and %> tags. The least frequently used tags are <%-- and --%> which, as you might guess, are simply comments that are never rendered to the resulting code.

Let's start with the simple Hello Python example. Under APACHE_HOME/htdocs create a file hello.psp with the following content:

<html>
<%
import sys
%>
Hello! You have <%=len(sys.modules)%> Python modules at your command.
</html>

Now go to http://127.0.0.1/hello.psp with your web browser. You will see how many Python modules are installed on your system. Of course, this information is trivial to us. Mod_python doesn't offer a function equivalent to PHP's phpinfo(), which gives detailed information about the interpreter. In the next step, we'll write one for PSP ourselves.

Under APACHE_HOME/htdocs, create a file pspinfo.psp with the following content:

<%%
req.content_type = 'text/plain'

import mod_python
import pprint
import sys
%>

PYTHON <%%=sys.version %>
MOD_PYTHON <%%=mod_python.version %>

PATH = <%%=pprint.pformat(sys.path)%>

AVAILABLE MODULES
<%%=pprint.pformat(sys.modules)%>            

Now open http://127.0.0.1/pspinfo.psp and you should see results similar to those shown below.

Figure 8

PSP uses Python code to output parts of the page using blocks which are distinguished by indentation (like Python itself). The last indentation will persists until either the end of the document or until another Python block. Take a look at the following example (blocks.psp).

<%
for i in range(5):
        j = 2**i
        # Loop block begins
%>
<%="%d<sup>%d</sup>=%d"%(2, i, j)%>
<%
# This will close the loop block
%>
Outside the loop block

In above example, I've used code blocks ( <% %>) and expressions ( <%= %>) together. When writing own scripts, please mind the difference between them: expression can contain only a single Python statement. If you find this notation hard to follow, let's revisit the Apache configuration from the previous step. Remember the AddHandler mod_python .psp .psp_ directive? Now we will make use of the debugging .psp_ address to see what's happening behind the scenes. When you go to http://127.0.0.1/blocks.psp you'll see PSP-produced Python Code and the source code, side by side.

Figure 9

This give an excellent in-depth look into the rendering engine. It also explains the logic behind mod_python engine: before execution, PSP pages get rendered into pure Python code.

The last information you need to begin developing basic PSP applications are sessions and form handling. Consider the following example of a basic login scenario (basic_login.psp). The password provided on the page should be a reversed UID:

<%
if 'uid' in form and 'passwd' in form and form['passwd']==''.join(reversed(form['uid'])):
        session['uid'] = form['uid']
%>
user <%=session['uid']%> logged in
<%
else:
%>
<form action="basic_login.psp">
UID<br/><input type="text" name="uid"/><br/><br/>
Password<br/><input type="password" name="passwd"/><br/><br/>
<input type="submit" value="Login"/>
</form>
<%
#
%>

PSP features two essential global variables, form and session. These are standard Python dictionaries. They give you maximum flexibility but be careful when assigning variables as they are not protected against being overwritten. You set session variables simply by setting key values of session dictionary variable:

session['variable'] = value

Note that Python does not limit you to use strings or integers as dictionary keys—any hashable value will fit. You can, for example, store a file object in the session pool to refer to it at any time during the session. Mod_python treats GET and POST requests equally, storing them in the form dictionary, making it easy to check whether the value has been set by the user. Python dictionaries have some very useful built-in methods, the most usable at this point are:

  • D.keys() - returns a list of dictionary D keys
  • D.has_key(k) - returns True if dictionary D has a key k, else False
  • D.get(k[, d]) - returns value of D[k] if D has key k, else returns d ( d defaults to None); can be very useful during form handling, e.g. <%=form.get('is_sure', 'not sure')%>

 

cx_Oracle

cx_Oracle Python module allows easy access to Oracle Database. It has been developed by Anthony Tuininga (Computronix) in conformance to the DB API 2.0 specification, which means you use the same set of methods and properties you can use to connect to MySQL or PostgreSQL.

Cx_Oracle has all the methods and properties of DB API 2.0 specification ( PEP 249) except the nextset() cursor method and Binary() constructor. Note however that cx_Oracle introduces many own extensions to the standard API, which greatly enhances working with Oracle databases. Each such extension is clearly marked in cx_Oracle documentation but basically, if you have already used DB API 2.0 in the past, you should feel like home.

 

Installing cx_Oracle

On Windows:
  1. Download cx_Oracle-4.1.2-win32-10g-py24.exe and double-click the downloaded installer.
  2. Click Next to continue.
  3. The installer will try to locate the default Python 2.4 interpreter. To accept the choice, click Next.
  4. To begin the installation process, press the Next button.
  5. Click Finish to complete installation.

On Linux:

  1. Download cx_Oracle-4.1.2-10g-py24-1.i386.rpm and place it in the /tmp directory of your system.
  2. Under /tmp directory run:
    $ rpm -ivh cx_Oracle-4.1.2-10g-py24-1.i386.rpm
    

Using cx_Oracle

As I've mentioned earlier, cx_Oracle is based on DB API 2.0 specification, which makes it extremely easy to use. Consider the following example to familiarize with the basic steps for connecting to the database and issuing queries. Create a file ora.psp under APACHE_HOME/htdocs to see whether the cx_Oracle module is working properly. (Replace username and passwords with values applicable to your configuration.):

<%
import cx_Oracle
db = cx_Oracle.connect('username', 'password', '127.0.0.1/XE')
c = db.cursor()

c.execute('select * from dual')
req.write(c.fetchall()[0][0])

db.close()
%>

When you open http://127.0.0.1/ora.psp you should see the X character in your browser as the only value in the system DUAL table.

Next Steps

You have just set up a complete working environment for PSP and Oracle Database 10g and also learned the basics of Python Web development. You can now follow this trail and begin writing more complex applications with PSP or switch to mod_python's Publisher Handler, which provides a better MVC (Model-View-Controller) strategy.

There are several application frameworks available for Python currently. They are not refined enough to be used in enterprise-grade environments, although I recommend you give them a try. Apache with mod_python itself is powerful enough to handle most of the development scenarios.


Przemyslaw Piotrowski is an information technology specialist working with emerging technologies and dynamic, agile development environments. Having a strong IT background that includes administration, development and design, he finds many paths of software interoperability.