Hacking with IronPython

I've been wanting to play with IronPython for a very long time, but never really got around to it since most of my days are either consumed with Python or Mono to some capacity, but never both.

Despite my initial instinct to flee in terror after looking over some of the IronPython examples I found on various blogs, I decided it would at the very least be worth an install just to check out the interpreter, and to see how well it performs on top of Mono.

  1. ccnet% ipy
  2. IronPython 1.0.2467 on .NET 2.0.50727.42
  3. Copyright (c) Microsoft Corporation. All rights reserved.
  4. >>>

Scary! But familiar, so I forged ahead undaunted, wanting to start hashing some strings, I figured I'd import the md5 module and get to work.

  1. ccnet% ipy
  2. IronPython 1.0.2467 on .NET 2.0.50727.42
  3. Copyright (c) Microsoft Corporation. All rights reserved.
  4. >>> import md5
  5. Traceback (most recent call last):
  6. File md5, line unknown, in Initialize
  7. File hashlib, line unknown, in Initialize
  8. File hashlib, line unknown, in __get_builtin_constructor
  9. ImportError: No module named _md5
  10. >>>

Alright, so there are still some holes in the IronPython bridge into Python, but this is fine by me, I can call into .NET code! One other thing that seemed to be missing was the 'select' built-in module, which in turn made my little 'telnetlib' based project fall on it's head.

I run an icecast2 server on my workstation, so I can just tune in with my MacBook Pro, and get whatever stream is being served up by the server. The source for the icecast2 server is a script using Liquidsoap which allows for shuffling, bumps, and a couple of other things to make my music-listening experience better. One of the nice things about Liquidsoap is that it has a telnet interface, so I can glean meta-data about what's playing, or control the playlist through the telnet interface. With this telnet interface in tow, I set out to hack up a Windows Forms and IronPython-based controller for already scripted radio station. And thus, my little IronRadio Controller was born:

IronRadio Controller

Unfortunately, I couldn't use Python's native "telnetlib" so I rolled my own IronTelnet class that would permit basic reads and writes to the telnet server, but other than that, the IronRadio Controller is mostly WIndows Forms code and some events cobbled together. The interface is unfortunately poor, as I don't have an Interface Builder for WIndows Forms, let alone IronPython-based Forms (not to mention I could care less about spit-and-polish for anything in X11.app).

The source for the script can be found here, and will require IronPython and Mono to run (or .NET if you're on a Windows machine).

I'm still trying to figure out if I can use IronPython with mod_mono to replace fighting with mod_python, but there are no guarantees as to whether that will work or be worth the trouble.

Tags:

Comments

md5

If you use a more recent version of IronPython (I suggest 1.1) then you should find that md5 is built in.

Nice entry.

Michael Foord
IronPython Cookbook

Using Native Python Modules

On Windows when you extract IronPython there is a site.py file in the Lib subdirectory. If you add in the following line:

  1. sys.path.append(r"Path to Python Lib Folder")

you can import native python modules from within IronPython. I havent tried this on my linux box so I dont know whether its the same on linux, but you could probably just drop the above sys.path.append into your scripts if you dont have a site.py (the site.py file is executed everytime you run ipy).

Hope this helps!

Kev

re: Using Native Python Modules

For what it's worth my site.py adds in both of the following:

  1. /usr/lib/python2.5/
  2. /usr/lib/python2.5/site-packages

(Off the top of my head, I can't connect to my Linux box at the moment)

While I can import some more regular modules like "socket" or "string", I can't load some other modules, which may be an issue fixed in IronPython 1.1 as the other commenter (Michael Foord) noted.