Jason in a Nutshell

All about programming and whatever else comes to mind

Archive for June 4th, 2009

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.

Posted in Programming | 3 Comments »