0

surely you have used a download manager. they detect and show the length of the file without downloading it

I know i can do this:


import requests
resp = requests.get("https://Whereever.user.wants.com/THEFILE.zip")
print(f"your file has {resp.headers['content-length'] \ 1048576}.")
...

but get downloads the content (THEFILE). so i can tell use the length after download. how to do that before download in python?

Thanks for detailed-answer

1

1 Answer 1

0

Instead of using GET request, do HEAD request:

resp = requests.request('HEAD', "https://Whereever.user.wants.com/THEFILE.zip")

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. In your case, where URL produces a large download, a HEAD request would read its Content-Length header to get the filesize without actually downloading the file.

3
  • I have seen that some servers don't support HEAD (some sayd this to me) am i write ??
    – 000Zer000
    Commented Oct 2, 2020 at 20:04
  • Perhaps some do not, but that would simply be stupid in general, as it would kill client-side caching which header based usually and that means higher traffic/load for the server. You can assume any non crappy server knows and supports HEAD. Commented Oct 2, 2020 at 20:07
  • 2
    cool . a great answer to my question in my comment. thanks bro
    – 000Zer000
    Commented Oct 2, 2020 at 20:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.