PyXPCOM gives Python access to Mozilla's XPCOM, cross-platform component object model.
This recipe demonstrates using PyXPCOM to access Mozilla's IDN service.
1 2 3 4 5 6 7 8 9 10
from xpcom import _xpcom from xpcom import components cls = components.classes['@mozilla.org/network/idn-service;1'] obj = cls.getService(components.interfaces.nsIIDNService) hangul_kr = u'\ud55c\uae00.kr' print obj.convertUTF8toACE(hangul_kr) # xn--bj0bj06e.kr
There seems to be severe lack of PyXPCOM examples on the net. It's a shame, since PyXPCOM is a great technology.
Note that this is just an example. I chose IDN because it's a very simple service, and is not related to HTML, XUL, or JS. I want to emphasize that XPCOM is a generic component architecture, not necessarily bound to Mozilla. However, as Python includes built-in support for IDN since 2.3, you should use that in real codes. See http://docs.python.org/lib/module-encodings.idna.html
One problem with PyXPCOM is installing it. Buliding PyXPCOM is a hair-splitting experience. You can still try it though. Consult Mozilla Developer Center for instructions: http://developer.mozilla.org/en/docs/PyXPCOM
If you are using Debian GNU/Linux like me, you will be happy to discover that Debian folks already took care of these. Just apt-get install python-xpcom and you are good to go.
XULPlanet has a nice XPCOM reference here: http://www.xulplanet.com/references/xpcomref/
Search for "IDN", and you will get a link to nsIIDNService interface. http://www.xulplanet.com/references/xpcomref/ifaces/nsIIDNService.html
hangul_kr is an IDN for Korean IDN registrar.
Note that despite the name, you don't need to convert Python unicode object to UTF-8 string. This is handled by PyXPCOM for you.