I have a BytesIO that I'm adding various bytes to. I want to send this in a urllib2.Request via the request.add_data method. How do I do this? When I try
# create request ....
bytesio = BytesIO()
bytesio.write(open("C:\img.jpg", "rb").read())
request.add_data(bytesio.getvalue())
bytesio.close()
urllib2.urlopen(request) # error "expected buffer, got bytes"
What am I doing wrong? I'm new to Python and not sure how to create a buffer from a BytesIO. Also, when I just try:
request.add_data(bytesio) # instead of bytesio.getvalue()
I get a "I/O operation on closed file". If I try to wait until after urlopen to call bytesio.close, then the request just hangs because it's waiting for bytesio to be closed.
What do I need to do?
Answer
request.add_data(str(btyesio.getvalue()))
bytesio.close()
Casting to a string made it happy. I haven't tried to see if it all works with StringIO and I haven't tried the differences between Python 2.x and 3.x.