pythonasynchronousasync-awaitaiohttpweb-scripting

Image doesn't download fully with aiohttp


I am using the following to get an image, but it's not properly downloaded and only a small top portion of image is seen. The file size is less than 256 kB. What's wrong and how could I fix it?

async with aiohttp.ClientSession() as session:
    async with session.get(url, timeout = 20) as response:
        if response.status == 200:
            image = await response.content.read(262144) # 256 kiB
        else:
            print("Check your URL!")
            return

Solution

  • The documentation says that read(n) reads up to n bytes but may return less. You'd have to call it in a loop until it returns an empty string indicating EOF.

    If you do not want to implement that loop (which is only useful if you have some buffering or streaming system of your own), just call read() without a size argument to receive all data in one go.