How do i convert from '_io.BytesIO' to a bytes-like object in python3.6?

Dan :

I am using this function to uncompress the body or a HTTP response if it is compressed with gzip, compress or deflate.

def uncompress_body(self, compression_type, body):
    if compression_type == 'gzip' or compression_type == 'compress':
        return zlib.decompress(body)
    elif compression_type == 'deflate':
        compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
        compressed = compressor.compress(body)
        compressed += compressor.flush()
        return base64.b64encode(compressed)

    return body

However python throws this error message.

TypeError: a bytes-like object is required, not '_io.BytesIO'

on this line:

return zlib.decompress(body)

Essentially, how do i convert from '_io.BytesIO' to a bytes-like object?

Thanks

wim :

It's a file-like object. Read them:

>>> b = io.BytesIO(b'hello')
>>> b.read()
b'hello'

If the data coming in from body is too large to read into memory, you'll want to refactor your code and use zlib.decompressobj instead of zlib.decompress.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert lists and dictionaries into bytes?

Convert from '_io.BytesIO' to a bytes-like object in python3.6?

Python3 How to make a bytes object from a list of integers

How do I convert a string into a string of bytes in python 2.7

How to convert bytes from Kafka to their original object?

How can I convert bytes object to decimal or binary representation in python?

How do I create a Python bytes object in the C API

csv file convert to io.BytesIO object, then stream to blob storage ,meets value type error:a bytes-like object is required, not '_io.TextIOWrapper'

Why I have to convert bytes from BytesIO then convert back to BytesIO so it can be read as PDF file response?

BytesIO like file object

How can I convert string to bytes in Python, like node js

How do I convert a string into bytes in python?

How do I convert line 6 and 7 from python into javascript?

Convert file into BytesIO object using python

Python convert xml to json a bytes-like object is required

how do i convert my supervisord configuration from python2 to python3?

How do I convert a list of integers into bytes?

Python3 TypeError: a bytes-like object is required, not 'str'

TypeError: a bytes-like object is required, not 'str' how do I tackle this?

How do I format this from str to byte-like object?

TypeError: a bytes-like object is required, not 'int' python3

How do I convert Pandas object to a list in python3

python3: TypeError: a bytes-like object is required, not 'str'

How do I convert a python object to (json) nested dict using the json module, without making a file-like object?

How do I convert a list of ASCII "Bytes" in Python into Hex Bytes

TypeError: a bytes-like object is required, not 'str' Using BytesIO

Tweepy, a bytes-like object is required, not str. How do i fix this error?

How do I convert a B string to bytes?

How do I convert a bytes object to a string in python?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive