Creating ASP Pages with Python
By Jeremy Lowery
Introduction
Ever since I first discovered ASP a few years ago, I have always loved the programming
paradigm that it implemented along with the accompanying technologies that helped ASP grow
into a very popular web-based solution (like ADO). However, I never really liked creating my
ASP pages with VBScript. For one, it is a very verbose language, and I'll do anything to save
a few keystrokes. Secondly, it contained no concept of object-oriented or even object-based
programming until version 5 with
its built-in class mechanism, which is a bit of a hack (see this
ASPMessageboard.com
post. And lastly, VBScript is nearly entirely immutable. Enter Python.
"What in the world is Python?" you ask. The official Python FAQ has the following to say:
"Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many brands of UNIX, on the Mac, and on PCs under MS-DOS, Windows, Windows NT, and OS/2."
Python FAQ: What is Python?
Some developers may not be aware of it, but ASP can be programmed in any language that has an ActiveScripting interface such as Microsoft's Jscript, or ActiveState's Perl. (To learn more about creating ASP pages with PerlScript, be sure to read: Using PerlScript to Create ASP Pages!) ActiveState also has a robust Python implementation that works very well on win32 systems. In this article, I will be introducing the use of Python in an ASP environment.
Advantages of Using Python
Here are a couple of things that I personally like about Python. Maybe you will too?
- Very easy syntatical structure
- Completely object oriented - Python supports all of the OO catch-pharses like custom object
creation(classes), multiple inheritence, and polymorphism. As a bonus, classes are 100%
mutable for runtime manipulation.
- Code encapsulation and reuse: By using namespaces, modules, and packages, Python
facilitates divide-and-conquer programming techniques and the packaging of frequently used code into
reusable modules.
- Named exception handling - Python supports true exception handling.
On Error Resume Next? Gimme a break.
- And finally, probably the most important perk... It's free!
Installing Python
The first thing to do is to get the latest binary of Python 2 from ActiveState's site.
You can get it from http://aspn.activestate.com/ASPN/Downloads/ActivePython/.
Please note that you need the Windows Installer 1.1 or higher to run this file. If you have
Windows 2000, you already have it. Otherwise you need to get it.
- For WinNT
http://download.microsoft.com/download/platformsdk/wininst/1.1/NT4/EN-US/InstMsi.exe - For Windows 98
http://download.microsoft.com/download/platformsdk/wininst/1.1/W9X/EN-US/InstMsi.exe
| Registering the Python ActiveX Scripting Engine |
|---|
The current build of Python seems to do this step for you automatically, but some older installs require that
you manually register the ActiveX Scripting Engine. This step must be done for you to build ASP pages using
Python as the server-side scripting language. If, when trying out some of the samples below, you get
an error message on the <%@ Language = Python %> line, attemp the following step:
Locate the This will allows the use of Python as a client or server-side scripting language (via Internet Explorer or an ASP page). |
Now it's time to test to see if the Python install worked. Create an ASP page in an appropriate Web-accessible directory with the following code:
|
Now visit the page you just created through your browser. You should see the various outputs displayed from the various client-side and server-side code snippets above. Congratulations, you've just written your first ASP page using Python!
Creating ASP Pages with Python
Using Python in ASP is as easy as using VBScript, given the fact that you have to learn a new
language if you don't already know Python. I highly recommend reading the Python
tutorial that comes with the binary distribution, it's the first and last online tutorial I've read on Python to
date. Below is a simple page that shows a good overview using Python and parsing form data.
|
The first thing is to tell ASP that the page's syntax should be interpretted with Python (as opposed to the
default, VBScript). To accomplish this, we use the @LANGUAGE directive:
<%@ Language = Python %>.
Also notice that the code is completely modular, that is: all code is defined inside of functions.
Functions are defined using the def statement. Also, unlike VBScript or JScript, code blocks in
Python are represented by indention. Just think of curly braces around the indented stuff. This is
unlike most ASP that is executed using the top-down approach. Of course, this is simply a
programming technique, but I think it makes the code more readable. I have also used Python's
multi-line string mechanism to output the HTML code.
|
This saves from changing execution context for the parser.
| Retrieiving Form Values |
|---|
Like JScript's undefined and null, Python has a special identifier named None.
When parsing form data, the following distinction is made:
Take the following QueryString:
|
In the above code example, after the data is retrieved, it is converted to a Python string using the built-in
function str(), so None becomes "None".
However, there is a better method to test if a form element exists. Use the
Request("element").Count property. Using this method, the main function definition would
become:
|
Finally, another interesting thing to notice about this script is the use of a default argument in the
definition for write_form(), a Python nicety sorely missing from VBScript.
Conclusion
As this article has illustrated, developers aren't restricted to using VBScript or JScript to create ASP pages -
they can also use Python (or PerlScript). This article also looked at creating a simple form processing
ASP script using Python. There are other neat things Python can do that we didn't look at explicitly in this
article. But if you are interested in learning more about Python be sure to read the tutorials at
Python.org and also check out the Calendar example script
available here.
Happy Programming!




