Python 3 support

Changed to python library to work both on Python 2 and Python 3 using the six library.
you could avoid needing `six` here with `from io import BytesIO`, which exists in 2.6+