To get the filesize of a download is really easy, servers usually provide a
Content-Length in its header response that let us know how heavy is the content we are requesting. We can find out this content length opening our shell and requesting a
HEAD response in linux:
As you can see, our content length is display in bytes. Let’s try to get this response with
Let’s use an image from httpbin. Remember to make a
HEAD request instead of a
GET request, this way we don’t have to download the entire file
'https://httpbin.org/image/png')req = requests.head(
When we filter the header with the
Content-Length key we can see how heavy our request will be without having to actually download the file.
This is a tricky one because, well, not always every web server is going to provide you with a
Content-Length in its header, this happens sometimes with csv, xml, and other text file-types, luckily they’re not that heavy and we can provide a
GET request to know the download size
'http://data.example.com/attachment_id=12')req = requests.get(
As we can see here, we can calculate a file size (mostly text files) using the len() method. If you have a better alternative you can show it off in the comments bellow.