Jason in a Nutshell

All about programming and whatever else comes to mind

Windows for Python programmers: IIS

Posted by Jason Baker on June 4, 2009

Ok, so you finally talked your employer into letting you use Python.  Good job!  But there’s a catch:  it has to run on Windows.  This actually isn’t too problematic.  Python runs very well on windows.  Unfortunately, it isn’t always terribly well documented.

IIS

Ok, now I know that you’re envious of those *nix Python programmers.  Apache running mod_wsgi is a pythonista’s dream.  And newer servers like lighttpd and nginx are a pythonista’s wet dream.  You might as well forget they exist:  Apache runs on Windows, but it just wasn’t made for it.  And forget about lighttpd and nginx.  You’re stuck with IIS.  Don’t worry, it’s really not that bad.

Options for running Python

This is the most important part for obvious reasons.  You have several options:

Classic ASP

No really, I’m being serious.  If you need a quick and dirty way to serve Python applications, classic ASP is the way to go.  You’ll need python win32, but chances are you’ll be needing that some time anyway.  I’d recommend just installing ActivePython.  Besides the obvious ease of setup, there is another benefit to using Python this way:  compatibility with other Classic ASP applications.  After all, you’re a windows shop.  You have at least one Classic ASP application lying around still, don’t you?

There is documentation here.

FastCGI

IIS’s FastCGI module is made and officially supported by Microsoft.  Thus, this is the best option for you if your management’s usual response to using open source software usually involves them saying something along the lines of “take a shower, hippie!”  Unfortunately, it’s a bit difficult to find documentation on (at least for Python) and a lot of Python software isn’t made to run as a daemon on Windows (django is still reliant on forking for example).  Thus, I’d recommend it as a last resort.

ISAPI-WSGI

Of course the biggest downside of this software is that it can be a bit of a tongue-twister:  say ISAPI-WSGI five times fast.  Other than that, I’ve had pretty good luck with it.  It’s easy to set up, and the performance isn’t too shabby.  Plus, it’s open source.  Currently, this seems to be the best way to get Python running in IIS.  Now you just need to convince your boss that you do in fact take showers.

There are instructions for getting ISAPI-WSGI set up with Django and Pinax, TurboGears and CherryPy, and Pylons.

PyISAPIe

Unfortunately, I don’t know enough about this project to be able to say enough one way or another.  But it is an option.

Miscellaneous tricks

Here are a few tricks that I’ve found to be helpful:

Use Application Pools

You may be envious of mod_wsgi’s daemon mode and its ability to run applications in their own process, but IIS 6 and above actually have something better:  Application Pools.  You have the ability to restrict certain sets of applications to a particular pool of processes.  This is handy if you need to isolate certain Applications.

This is also handy because if you make any changes, you will need to restart the Python interpreter.  If you’ve got Python running on its own server, then you have nothing to worry about.  Otherwise, you’ll want a way to restart the Python interpreter without disturbing any other applications that may be running.  This is where Application Pools really shine because you can not only restart Python separately from the rest of the server, but you can also restart individual Python applications as well!

To learn more about creating Application Pools, see this technet article.  Also check out this article to get more info about how to recycle (restart) an Application Pool.

Use ActivePython

I’ve already mentioned this once, but it bears mentioning again.  Use ActivePython.  It comes pre-bundled with Python win 32, which you’ll probably be installing anyway.

Choose a good framework

I suppose I could probably write a separate blog post about choosing web frameworks, but that’s for a different time.  The good news is that I can sum it up by saying this:  if you’re not sure, choose Django.

About these ads

3 Responses to “Windows for Python programmers: IIS”

  1. Acknowledge that Apache/mod_wsgi on Windows has no daemon mode and that that isn’t about to change, so application pools in IIS do sound quite interesting.

    For those who don’t have a choice and have to use Apache, worthwhile pointing out:

    http://blog.dscpl.com.au/2009/02/source-code-reloading-with-modwsgi-on.html

    http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Restarting_Windows_Apache

    One of the features of daemon mode of mod_wsgi is the ability to easily have the application be reloaded without having to restart Apache as a whole. One can’t avoid that on Windows, but with the recipe described in those links, one can use some slight of hand to at least make it automatic during the development phase.

  2. Jason Baker said

    Didn’t know that. Thanks for bringing it up!

  3. An don’t forget IronPython… A good choice for .NET shops.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: