Back in 2004 I wrote a little Python-based web application to do XML-style search of my blog entries. It was a laboratory in which I studied structured search, microformats, in-memory data, and lightweight web applications.
Today I converted that application to IronPython. My purpose is to explore what can be done with the combination of IronPython and the .NET Framework.
I’ve reconstituted the original CPython-based searcher here:
The new IronPython-based searcher is here:
They look just the same, but you can tell which is which by looking in the browser’s title bar. One says CPython, the other IronPython.
Both are running on a Windows Server 2003 box — the same one, actually, that’s been running the CPython version for the past few years.
The code’s mostly the same too, except for the infrastructure parts. The CPython version uses the simple BaseHTTPServer that comes with Python, and it uses libxml2 for XML parsing and and libxslt for XSLT transformation. The IronPython version, instead, uses the .NET Framework’s built-in webserver (System.Net.HttpListener) and XML facilities (System.Xml).
It’s pretty much an apples-to-apples comparison, as far as these things go. Neither version is, or pretends to be, robust or scalable. Both are using bare-bones HTTP implementations in single-threaded mode, which is a technique that I find perfectly appropriate for lots of handy little services and applications that are used lightly.
The two versions seem to perform about the same on most queries as well, though the IronPython version is way faster when you use the box labeled “all paragraphs containing phrase”.
So what’s the point of this exercise? It demonstrates an unusual approach to using .NET, one that bridges between two very different cultures. In the open source realm, an enormous amount of work gets done in dynamic languages that leverage components, or modules, or libraries, to do the heavy lifting in areas like HTTP and XML. But it’s a big challenge to integrate Python with, say, libxml2, and it’s that same challenge all over again when you want to connect PHP or Ruby to libxml2.
Meanwhile, in the realm of Microsoft-oriented development, most of the work is being done in statically-typed languages. These languages also rely on components, or modules, or libraries to do the heavy lifting. But they can more effectively share the common heavy-lifting capability that’s in the .NET Framework.
The best of both worlds, I believe, is dynamic languages fully connected to common infrastructure. I’m not alone in thinking that, and the Python/.NET combo is not the only way to get there. Sean McGrath has said:
Jython, lest you do not know of it, is the most compelling weapon the Java platform has for its survival into the 21st century. 
Today’s experiment confirms my hunch that IronPython will be at least as compelling, and will open up the .NET Framework to lots of folks for whom the traditional methods of access aren’t appealing.
There was one fly in the ointment. I had wanted to host this IronPython application on the Windows Communication Foundation (WCF) which would provide a much more robust engine than System.Net’s HttpListener. And at first it looked like it would work. But WCF service contracts require the use of a .NET feature called attributes. It turns out there isn’t yet a way to represent those in IronPython. If someone has figured out an intermediary that enables IronPython to implement RESTful WCF services, I’d love to see how that’s done.
14 thoughts on “My first IronPython application”
I scripted some lightweight utilities for WinXp using ActivePython 2.4 plus Mark Hammond’s win32 API:-
In your experience Jon, does this new .NET stuff now make that approach obsolete or just old fashioned?
Maybe you should try Boo language then, it is a .NET/CLR language with a Python-inspired syntax too, but in the contrary of IronPython, Boo supports pretty much everything in .NET (except unsafe code) : attributes, delegates, generics, etc :)
“does this new .NET stuff now make that approach obsolete or just old fashioned?”
Neither, I don’t think. Mark’s stuff is brilliant and will continue to do what it does for a very long time. From your perspective I guess it’d be a case of, as and when parts of the Framework become compelling, for one reason or another, then you’d like to be able to access them from Python.
Actually I’d be interested to hear from you which parts of the Framework are appealing. Obviously it is a big tent. Most of the IronPython examples so far are in the rich-client domain, where there’s actually a better story about integration between IronPython and the Framework than there currently is in the services domain.
“Maybe you should try Boo language then”
Maybe I should. Is there a recipe (http://boo.codehaus.org/Recipes) for working with Indigo?
At work we use IronPython. We have needed to access unmanaged code – which requires attributes, so we’ve had to write stub classes in C#.
What I’d love to do is automate the generation of these ‘stub classes’, using the Reflection.Emit API – I haven’t had the time to do this yet, but I’ll get to it sometime…
If you like dynamic languages on .NET, you might also enjoy looking at Vista Smalltalk which runs on .NET servers and displays in WPF/e. It includes Lisp as well.
Peter Fisk’s blog entries have working demos of his work in progress. These include using a class browser to edit classes in the IE browser. He provides screenshots of each example of his development to date as well as commentaries on why dynamic languages will fill an important gap in development tools on top of WPF & .NET.
“you might also enjoy looking at Vista Smalltalk”
James Governor pointed me to that. It’s a jaw-dropper!
Jon: What I’d REALLY like to hear about is how you set up both CPython and IronPython on Windows 2003 Server. Did you use ActiveState for CPython, or something else? On IIS, or Apache? What about IronPython? On IIS? If so, how are you getting around not having to use file extensions on your URLs?
Please address this! It’s the one thing keeping me from trying out both CPython and IronPythin for web programming…
“Did you use ActiveState for CPython?”
“How are you getting around not having to use file extensions on your URLs?”
I guess there are lots of ways. In my case, I’m using the simple single-threaded webservers that are built into the frameworks as mentioned above.
“I guess there are lots of ways. In my case, I’m using the simple single-threaded webservers that are built into the frameworks as mentioned above.”
Thanks for the reply. Does that mean you are *not* using IIS? If not, does that mean you are *just* using (System.Net.HttpListener) and XML facilities (System.Xml)?
If so, any chance you could publish a blog post about a simple “Hello World” IronPython example complete with a ready-to-roll download and an explanation as to how to get it up and running, and anything one would need to turn off or configure to make it work on both XP and 2003 Server? I’d be very grateful….
P.S Hey, your blog needs a Jon Udel favicon instead of the generic WordPress one! :)