To read the article online, visit

Creating ASP Pages with Python

By Jeremy Lowery

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 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?

  1. Very easy syntatical structure
  2. 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.
  3. 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.
  4. Named exception handling - Python supports true exception handling. On Error Resume Next? Gimme a break.
  5. 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 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.

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 python\win32com\AXScript\Client directory, and double-click (or alternately enter and run python from the command line.)

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:

<%@ Language = Python %>
  Response.Write("Python lives in the ASP delimeters!")
<br />

<script language="Python">
document.write("Python's throwing a party on the client-side!")
<br />

<script language="Python" runat="server">
Response.Write("Python gets ready to rumble inside a server-side scripting block!")

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.

<%@ Language = Python%>
def write_form(msg = ""):
		<title>Hi there!</title>
	""" + msg + """
	<form>What is your name? 
		<input type="text" name="txtName" /> 
		<input type="submit" name="btnSubmit" value="Click Me" />

def parse_form_data():
	name = str(Request("txtName"))
	if name == "" :
		write_form("Please fill in a name.")
	Well hello there """ + name + """.

def main() :
	sub = str(Request("btnSubmit"))
	if sub != "None" and sub != "" :
	else :

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 is
a multi-line string.

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:

  • If the form's data is not found, None is returned.
  • If the form's data is blank, the empty string ("") is returned.

Take the following QueryString:

Request("foo") will return "" while Request("tinytim") will return None.

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:

def main() :
	if Request("btnSubmit").Count != 0:
	else :

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.

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 and also check out the Calendar example script available here.

Happy Programming!

  • By Jeremy Lowery

  • Article Information
    Article Title: Creating ASP Pages with Python
    Article Author: Jeremy Lowery
    Published Date: Wednesday, August 22, 2001
    Article URL:

    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers