substitute StringIO for stream_class if content is an instance of six.text_type, otherwise substitute BytesIO for stream_class.
stream_class = StringIO if isinstance ( content , six . text_type ) else BytesIO