#!/usr/local/bin/python # -*- coding: iso-8859-1 -*- # # idna # # Oliver Fromme # # This Python program converts Latin1 domain names # to their IDNA representation and vice versa. # See RFC 3490 and RFC 3492. # # Please refer to the excellent Python docs for details! # (Especially, of course, the Python Library Reference.) # # http://www.python.org/doc/current/ # import sys if not hasattr(sys, "hexversion") or sys.hexversion < 0x20300f0: sys.stderr.write("idna: Sorry, need at least Python 2.3.\n") sys.exit(1) # # This function converts a single component "s" # from one encoding to another. # def convert (s, dec, enc): if not s: return "" try: return s.decode(dec).encode(enc) except: return "" % (dec, enc) # # For a complete hostname, find out the conversion direction, # perform the conversion and convert the result. # def convert_name (name): src, dst = "LATIN1", "IDNA" if name.startswith("xn--") or name.find(".xn--") >= 0: src, dst = dst, src return ".".join(map(lambda s: convert(s, src, dst), name.split("."))) # # If there are any command line arguments, convert each of them. # Otherwise read names from stdin, separated by whitespace, # and convert each of them. # # In any case, the results are printed one per line. # if len(sys.argv) > 1: for i in sys.argv[1:]: print convert_name(i) else: for line in sys.stdin: for i in line.strip().split(): if i: print convert_name(i) #--