python写的带进度条的下载文件的脚本

#!/usr/bin/env python

-- coding: utf-8 --

import urllib
from progressbar import *

urlfile=’http://xx.com/a.rar
localfile=’download.rar’

def cbk(a, b, c):
‘’’’’回调函数
@a: 已经下载的数据块
@b: 数据块的大小
@c: 远程文件的大小
‘’’
w=[‘Progress: ‘, Percentage(), ‘ ‘, Bar(marker=RotatingMarker(‘>-=’)),’ ‘,
ETA(), ‘ ‘, FileTransferSpeed()]
pbar=ProgressBar(widgets=w, maxval=100).start()

per = 100.0 * a * b / c  
if per < 100:  
    pbar.update(int(per))
else:
    pbar.update(int(per))
    pbar.finish() 

if name == ‘main‘:
urllib.urlretrieve(urlfile,localfile,cbk)
print ‘Download Ok,Saved to %s.’%localfile
 

运行结果如下:

netcat@netcat:~/python$ python url_down.py
Progress: 100% |>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>| Time: 0:00:00 812.85 kB/s
Download Ok,Saved to download.rar.