Skip to content

Instantly share code, notes, and snippets.

@code
Created February 23, 2009 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code/68841 to your computer and use it in GitHub Desktop.
Save code/68841 to your computer and use it in GitHub Desktop.
# Got hit by this crazy bug in python
result = urlfetch.fetch('http://domain/image.jpg')
multipart_upload_body = "multi part %s upload string" % result.content
# This caused an invalid ascii error since result.content is binary and python wanted to convert it to ascii.
# The solution was not obvious, the only way we found to do it was to create a byte array then write to it like a buffer
buffer = array.array("B") # B = unsigned bytes
buffer.fromstring("multi part ")
buffer.fromstring(result.content)
buffer.fromstring(" upload string")
multipart_upload_body = buffer.tostring()
# works! string handling in python 2.x is a little odd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment