Make script portable across Python versions

Support both Python 2 and 3.