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

AMC
  • 2,642
  • 7
  • 13
  • 35
000Zer000
  • 35
  • 1
  • 9
  • Does this answer your question? [Get size of a file before downloading in Python](https://stackoverflow.com/questions/5909/get-size-of-a-file-before-downloading-in-python) – AMC Oct 03 '20 at 00:23

1 Answers1

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I have seen that some servers don't support HEAD (some sayd this to me) am i write ?? – 000Zer000 Oct 02 '20 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. – Marcin Orlowski Oct 02 '20 at 20:07
  • 1
    cool . a great answer to my question in my comment. thanks bro – 000Zer000 Oct 02 '20 at 20:08