简体   繁体   中英

BytesIO from url using open()

Can you help me , I need to open my_url in rb mode. Try to do this.

 url = "https://my url/" + file_info.file_path
            response = requests.get(url)

            with open(BytesIO(response.content), "rb") as f:  # Open in 'rb' mode for reading it in way like: 010101010
                byte = f.read(1) 
                #some algorithm..............
                while byte:
                    hexadecimal = binascii.hexlify(byte)
                    decimal = int(hexadecimal, 16)
                    binary = bin(decimal)[2:].zfill(8)
                    hiddenData += binary
                byte = f.read(1)

Have an error:

Expected str,bytes or.osPathLIke object, not _ioBytesIO

Can you help ,please, how I should open my url in "rb" mode?

I was trying to open an image, using Pillow - it is okay. But as for using open() , I can not do the same. Please..

you're passing a BytesIO object (basically a file handle) where a filename is expected.

So quickfix:

f = BytesIO(response.content) 

but better, iterate on a bytes objects using iter either manually (for the start of your algorithm) or automatically (using a for loop which will stop when the iterator is exhausted, so no need for while ):

f = iter(response.content)

byte = next(f)

#some algorithm..............
for byte in f:
    hexadecimal = binascii.hexlify(byte)
    decimal = int(hexadecimal, 16)
    binary = bin(decimal)[2:].zfill(8)
    hiddenData += binary

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