从yum源下载软件包
以下是从163源下载openstack-ocata版软件包的脚本:
from html.parser import HTMLParser
from urllib import request
import urllib
import os,sys
import socket
class myparser(HTMLParser):
'''找到a标签并把属性的值放到列表里'''
def __init__(self):
HTMLParser.__init__(self)
self.links = []
def handle_starttag(self, tag, attrs):
if tag == 'a':
if len(attrs) == 0:
pass
else:
for (variable,value) in attrs:
# print(value)
if variable == 'href':
self.links.append(value)
def callbackfunc(blocknum, blocksize, totalsize):
'''回调函数,打印下载进度
@blocknum: 已经下载的数据块
@blocksize: 数据块的大小
@totalsize: 远程文件的大小
'''
percent = int(100.0 * blocknum * blocksize / totalsize)
if totalsize > 505528:
pass
else:
percent = 100
sys.stdout.write('\r')
sys.stdout.write(file_name + percent * '>' + str(percent) + '%')
sys.stdout.flush()
def create_dir(root_tree,catalog):
'''根据url的目录结构在本地穿件文件夹'''
os.chdir(root_tree)
try:
os.makedirs(catalog)
except FileExistsError as e:
pass def download_file(url,down_path):
'''下载文件保存到相应的目录,并把下载失败的放在一个字典里'''
global file_name
global error_download
file_name = url.split('/')[-1]
error_download = {}
socket.setdefaulttimeout(30)
try:
request.urlretrieve(url,down_path,callbackfunc)
except socket.gaierror as e:
error_download[url] = down_path
print('socket.gaierror' , url)
except urllib.error.URLError as e:
error_download[url] = down_path
print('urllib.error.URLError',url)
sys.stdout.write('\n') def get_url_tree(url_tree):
'''获取一个字典,链接:目录,并把文件夹创建及把文件下载'''
url_tree_dict = {}
level = 0
for url in url_tree:
response = request.urlopen(url)
page = response.read().decode('utf-8')
hp = myparser()
hp.feed(page)
hp.close()
try:
hp.links.remove("../")
except ValueError as e:
pass
for file in hp.links:
if '/' in file:
create_dir(url_tree[url], file)
url_tree_dict[url+file] = url_tree[url]+file
else:
download_file(url+file,url_tree[url]+file)
if file.find('/') > 0:
level += 1
return url_tree_dict , level
url_tree = {"http://mirrors.163.com/centos/7/cloud/x86_64/openstack-ocata/":'/centos/7/cloud/x86_64/openstack-ocata/'}
try:
os.makedirs('/centos/7/cloud/x86_64/openstack-ocata/')
except FileExistsError as e:
pass while True:
url_tree,level = get_url_tree(url_tree)
if level == 0:
break
print(url_tree,level)
for key in error_download:
download_file(key,error_download[key])
从yum源下载软件包的更多相关文章
- centos7最小安装后——网络配置、常见命令安装,远程连接、yum源安装软件包
安装环境 #软件:vmware 14 #centos版本:CentOS-7-x86_64-DVD-1810 下载地址: #网络配置:NAT模式 配置 网络配置 #动态获取ip: centos7最小安装 ...
- Docker:Centos7更新yum源下载docker
前言: Docker 要求 CentOS 系统(6.5及以上)的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname -r 命令查看你 ...
- 从yum源下载安装包及依赖包
局域网内所有linux都用yum从外网源安装软件有点浪费,尤其遇到下载慢的情况: 所以考虑下载后传到其他机器安装,还可以保证版本一致(创建一个本地仓库更好,这个后面研究了再记录): 首先安装yum工具 ...
- Linux 使用本地yum源及软件包管理
[root@node130 rh]# pwd/opt/rh[root@node130 rh]# lsrhel-server-6.4-x86_64-dvd.iso [root@node130 rh]#m ...
- testng+IEDriverServer+yum+tomcat下载软件包
testng框架链接:http://files.cnblogs.com/files/linxinmeng/testng%EF%BC%88selenium%E6%A1%86%E6%9E%B6%EF%BC ...
- CentOS 5/6.X 使用 EPEL YUM源
参考:http://www.linuxidc.com/Linux/2013-08/88523.htm 大纲 一.什么是EPEL? 二.与163 YUM源比较 三.CentOS 5.X 安装使用EPEL ...
- 使用yum来下载RPM包而不进行安装
1. 安装yum-downloadonly. yum-utils 或 yum-plugin-downloadonly 软件包 (RHEL5) # yum install yum-downloadonl ...
- CentOS下安装yum源的流程和操作
一般公司都用Linux来搭建服务器,Linux安装软件时能够用yum安装依赖包是一件非常简单而幸福的事情,因为你只需一个简单的安装命令yum install []即可安装相应的软件,yum工具会自动的 ...
- Linux升级python3之后yum不能正常使用解决方法一:重新配置yum源
[转]linux下yum安装及配置 分步阅读 公司使用的是linux搭建服务器,linux安装软件能够使用yum安装依赖包是一件非常简单而幸福的事情,所以这里简单介绍一下linux安装yum源流程和操 ...
随机推荐
- cf #363 c
C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- redhat5.8系统学习
# redhat5.8系统学习 ### 简介-----------------------------redhat操作系统是红帽公司的收费版操作系统 ### 查看系统版本号-------------- ...
- iOS多线程(转)
关于iOS多线程,你看我就够了 字数8596 阅读28558 评论74 喜欢313 在这篇文章中,我将为你整理一下 iOS 开发中几种多线程方案,以及其使用方法和注意事项.当然也会给出几种多线程的案例 ...
- nginx 中location和root,你确定真的明白他们关系?
最近公司开发新项目,web server使用nginx,趁周末小小的研究了一下,一不小心踩了个坑吧,一直404 not found!!!!!当时卡在location和root中,但是网上却比较少聊这方 ...
- 解决PHP编译cURL的reinstall the libcurl问题
今天正好要用到PHP的curl功能,发现服务器上的PHP并没有配置curl,进而查询PHP官方文档,得知编译PHP时需要带上 –with-curl参数,才能把curl模块编译进去.我现在PHP已经编译 ...
- Redis Scan的使用方式以及Spring redis的坑
SpringRedisTemplate针对这个Scan进行了封装,示例使用(针对最新库spring-data-redis-1.8.1.RELEASE): Set<Object> execu ...
- Beamer加中文
\documentclass{beamer} \mode<presentation> { \usetheme{CambridgeUS} % or try Darmstadt, Madrid ...
- 离散数学及其应用(Discrete Mathematica With Application 7th)学习笔记 第一章
目前本人只进行到了第五章的章末补充练习,应该是从4月6号开始学习的,又是英文版,而且基本就下班回家抽2个小时左右去学,所以进度较慢. 由于本质是数学,除了一些程序处理和大计算量的问题,基本上一本草稿本 ...
- Android Otto框架浅析
今天要介绍的是一个Android中使用得比較多的android 事件总线 EventBus模式的一个框架Otto. Otto 官网:http://square.github.io/otto/ 一.An ...
- 获得String形式日期的后一天
try { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd"); Calendar c = Calendar.ge ...