using cStringIO for better buffering performance