1 #!/usr/bin/python2 #Encoding:utf-83 ImportUrllib4 ImportOS5 defSchedule (a,b,c):6 " ""'7 A: Data blocks that have already been downloaded8 B: Size of the data block9 C: The size of the remote fileTen " " Oneper = 100.0 * A * b/C A ifPer > 100 : -per = 100 - Print '%.2f%%'%per theURL ='http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2' - #local = Url.split ('/') [-1] -Local = Os.path.join ('/data/software','python-2.7.5.tar.bz2') - Urllib.urlretrieve (url,local,schedule) + ## # # # #output ###### - #0.00% + #0.07% A #0.13% at #0.20% - #.... - #99.94% - #100.00%
Let's take a look at the Urlretrieve () function provided by the Urllib module. The Urlretrieve () method downloads remote data directly to the local.
1 |
>>> help(urllib.urlretrieve) |
2 |
Helpon function urlretrieve inmodule urllib: |
4 |
urlretrieve(url, filename=None, reporthook=None, data=None) |
- The parameter finename specifies that the local path is saved (Urllib generates a temporary file to hold the data if the parameter is not specified.) )
- The parameter reporthook is a callback function that triggers the callback when the server is connected and the corresponding data block is transferred, and we can use this callback function to display the current download progress.
- The parameter data refers to a post to the server, which returns a two-element (filename, headers) tuple, filename, which represents the local path, and the header represents the server's response header.
Refer to HTTP://BLOG.CSDN.NET/BONE_ACE/ARTICLE/CATEGORY/3039643/3
Urlretrieve () function resolution for Python urllib (show download Progress)