0

I am reading some codes which work nicely. In the codes, it use urlretrieve to download file from web. While it's downloading it can also report how much has been downloaded.

The urlretrieve calling is like below:

urlretrieve(url + filename, dest_filename, reporthook=download_progress_hook)

And the download_progress_hook() is defined as below:

def download_progress_hook(count, blockSize, totalSize):
    print(count, blockSize, totalSize)

When I gave some url and started to download sth. from web, it kept reporting count,blocksize and totalsize as below output

0 8192 8458043
1 8192 8458043
2 8192 8458043
3 8192 8458043
4 8192 8458043
5 8192 8458043
6 8192 8458043
7 8192 8458043

The python documents described as "The hook will be passed three arguments; a count of blocks transferred so far, a block size in bytes, and the total size of the file. The third argument may be -1 on older FTP servers which do not return a file size in response to a retrieval request." This looks strange for me. My understanding is when urlretrieve functions was calling, it used the output of download_progress_hook function as the input to urlretrieve and named it as reporthook variable. But according to the results of this code, it looks like whenever urlretrieve is called with 3rd arguments, which should be defined as function, the urlretrieve will feedback the 3 information to that function. I know from other functions in urllib, we might get the file size by checking the header file. But I don't understand how this whole mechanism worked here. How the variable passed into function download_progress_hook. According to output, it's like the download_progress_hook function are keeping running during the lifetime of urlretrieve. This might be some concepts in python programming or urllib? Appreciating any thoughts and instructions.

XXWang
  • 33
  • 2
  • 9
  • Possible duplicate of [Why function works properly without specifying parameters?](https://stackoverflow.com/questions/43370284/why-function-works-properly-without-specifying-parameters) – Muaaz Khalid Nov 14 '17 at 18:36

1 Answers1

1

This is late reply but since i also was reading about this answered here https://stackoverflow.com/questions/43370284/why-function-works-properly-without-specifying-parameters/

summary:

urlretrieve doesn't call download_progress_hook. It merely gives that function object to the urlretrieve() function as reference and it is that code that'll call download_progress_hook somewhere (passing in the required arguments).

Amir
  • 23
  • 5