今天用chrome下载ubuntu系统iso的时候非常慢(事实上用chrome下载什么都经常异常的慢),但是又没有安装迅雷(也不想),于是打算自己用python写一个下载器。
代码很简单,没有断点续传,没有文件校验,更没有p2p,就一个下载的功能;
# coding: utf-8
import urllib
url = 'http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso'
filename = url[url.rindex('/'):]
print('filename = ' + filename)
downloaded = '0'
def download_listener(a, b, c):
per = 100.0 * a * b / c
if per > 100:
per = 100
new_downloaded = '%.1f' % per
global downloaded
if new_downloaded != downloaded:
downloaded = new_downloaded
print 'download %s%% %s/%s' % (downloaded, a * b, c)
response = urllib.urlretrieve(url, 'D:\\download\\' + filename, download_listener)
因为技术含量极低,所以不得不说,python大法真好……