简体   繁体   中英

How do I base64 encode a buffered read in Python?

I'm streaming a png to the browser for use in an img src attribute.

def streamer():
        with open(local_path) as fh:
            while True:
                rbuf = fh.read(READ_BUF_SIZE)
                if not rbuf:
                    break
                yield rbuf

I want to do something like this:

base64.b64encode(streamer())

But I'm not able to encode a generator.

for part in streamer():
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    print base64.b64encode(part)
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

base64.b64encode requires bytes or a string (depending on the Python version).

If you don't mind reading the entire file into memory, first create the full string and then encode it:

base64.b64encode(''.join(streamer()))

If you want to encode as you go, just be careful to encode in multiples of three bytes. (Three bytes becomes four characters in base64, so you won't have padding issues.)

Eg, using a multiple of three as READ_BUF_SIZE , this should work:

for part in streamer():
    yield base64.b64encode(part)

The last part should be the only one that's not a multiple of three bytes, so it should be the only one that gets padding (as expected).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM