需要批量下载安装程序的可以参考以下.
实现方法:
1.urls.log中保存需要下载的deb包的URL地址
2.download-install.py从urls.log中解析URL,然后下载到./Downloads目录下,最后逐个安装.
3.执行: $python -i download-install.py 可以看到下载及安装过程
上代码:
#!/usr/bin/env python
import urllib
import os
def Schedule(a,b,c):
per = 100.0 * a * b / c
if per > 100:
per = 100
print '%.2f%%' % per
def parse_urls():
ls = []
f = open('urls.log')
lines = f.readlines()
for line in lines:
#print line
ls.append(line.strip())
return ls
def download_file(url):
print "downloading: ", url
path = url.split('/')
local = "./Downloads/" + path.pop()
print "local_name: ", local
urllib.urlretrieve(url, local, Schedule)
def get_files(tar_list):
print tar_list
if os.path.exists('./Downloads') != True:
os.system('mkdir Downloads')
for url in tar_list:
download_file(url)
def install_deb():
deb_dir = os.getcwd() + "/Downloads"
for parent, dirnames, filenames in os.walk(deb_dir):
for filename in filenames:
path = deb_dir + "/" + filename
ret = os.system('sudo dpkg -i ' + path)
if ret != 0:
print "install failed:", path
if __name__ == '__main__':
urls = parse_urls()
#print urls
get_files(urls)
install_deb()
urls.log文件格式:
代码下载地址:http://download.csdn.net/detail/foxbryant/9754509