python实现下载网站补丁包,并定时清理
场景说明:
1、macfee杀毒软件会定时更新补丁包,需要去官网下载,客户手动更新补丁包;
2、需要定时清理补丁包,以免占用磁盘空间过大;
3、需要有下载的日志记录。
程序功能:
1、使用urllib2,urllib类从网站抓取数据,并下载到指定路径;
2、为避免重复下载,在下载前做数据对比;
3、使用多线程,一个实现下载的功能,另一个实现清理功能;
4、每24小时执行一次。
import urllib2,urllib
import re
import os,sys
import time
import datetime
import threading
proxy_info={'user':'dc\user', 'password':'xxxxxx' , 'server':'http://xxx:8080'}
url1 = "http://download.nai.com/products/licensed/superdat/nai/Chinese/simplified/"
path=r'e:\mcafeepatch'
con=threading.Condition()
def downloadpatch(path,url1):
if con.acquire():
while 1:
print ' start thread of downloadpatch'
print 'present time is: ',datetime.datetime.now()
passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmgr.add_password(None, proxy_info['server'] , proxy_info['user'], proxy_info['password'])
auth = urllib2.ProxyBasicAuthHandler(passmgr)
opener = urllib2.build_opener(urllib2.ProxyHandler({'http':proxy_info['server']}) , auth)
urllib2.install_opener(opener)
proname = urllib2.urlopen(url1)
text=proname.read()
print 'connect to website successfully'
#print text
name=re.findall('HREF="(\d+xdat.exe)"',text,re.IGNORECASE)
#name=re.findall('HREF="(readme.txt)"',text,re.IGNORECASE)
print 'the following files are all patchs in the website: '
print name
files=os.listdir(path)
for i in files:
for x in name:
if i==x:
name.remove(x)
if len(name)>0:
print 'the following files are need to download:'
print name
print 'please wait......'
for i in name:
f=open(path+'\\'+i,'wb')
downpro=urllib2.urlopen(url1+i)
while 1:
data=downpro.read(1024)
if not len(data):
break
f.write(data)
f.close()
print '%s files have download!!!'%i
f1=open(path+'\\'+'log'+'\\'+'log.txt','a')
f1.write(str(datetime.datetime.now())+' ')
f1.write('%s files have download!!!'%i)
f1.write('\n')
f1.close()
else:
print 'no files have to download'
proname.close()
print '--------------------------------------------'
con.notify()
con.wait()
time.sleep(24*60*60)
#time.sleep(10)
def deletepatch(yourpath):
if con.acquire():
while 1:
print ' starting thread of delete files'
print 'present time is :',datetime.datetime.now()
pathlist=os.listdir(yourpath)#list all files
for i in range(len(pathlist)):#counts
source=yourpath+'\\'+pathlist[i]#path of a file
if os.path.isfile(source):#whether is file
m=time.localtime(os.stat(source).st_ctime)# create time of file
endtime=datetime.datetime.now()# now time
startime=datetime.datetime(m.tm_year,m.tm_mon,m.tm_mday,m.tm_hour,m.tm_min,m.tm_sec)
#translate the time
mydays=(endtime-startime).days
if mydays>=7:#if time is over 7 days
os.remove(source)# remove the file
print 'File',source,'have been deleted'
f2=open(path+'\\'+'log'+'\\'+'log.txt','a')
f2.write(str(datetime.datetime.now()))
f2.write('File',source,'have been deleted')
f2.write('\n')
f2.close()
else:
print 'File',source,'is now useful for us'
else:
print 'File',source,'is not execute program'
print '--------------------------------------------'
con.notify()
con.wait()
time.sleep(24*60*60)
#time.sleep(10)
if __name__=='__main__':
try:
t1=threading.Thread(None,target=downloadpatch,args=(path,url1))
t1.start()
t2=threading.Thread(None,target=deletepatch,args=(path,))
t2.start()
except Exception,e:
print e

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
LeetCode 202: 快乐数 Happy Number
题目: 编写一个算法来判断一个数是不是 “快乐数”。 一个 “快乐数” 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this p...
-
下一篇
阿里最新38道Java面试题解析(MyBatis+消息队列+Redis)
一、谈谈你对 MyBatis 的理解? Mybatis是一个半ORM(对象关系映射)框架,它内部封装了 JDBC,开发时只需要关注 SQL 语句本身,不需要花费精力去处理加载驱动、创建连接、创建 Statement 等繁杂的过程。程序员直接编写原生态 SQL,可以严格控制 SQL 执行性能,灵活度高。 MyBatis 可以使用 XML 或注解来配置和映射原生信息,将 POJO 映射成数据库中的记录,避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。3. 通过 XML 文件或注解的方式将要执行的各种 Statement 配置起来,并通过 Java 对象和 Statement 中 SQL 的动态参数进行映射生成最终执行的 SQL 语句,最后由 MyBatis 框架执行 SQL并将结果映射为 Java 对象并返回。(从执行 SQL到返回 Result 的过程)。 二、MyBaits 的优缺点有哪些? § 优点: 基于 SQL 语句编程,相当灵活,不会对应用程序或者数据库的现有设计造成任何影响,SQL 写在 XML 里,解除 SQL 与程序代码的耦合,便于统一管理;提供XML标签,...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- MySQL数据库在高并发下的优化方案
- CentOS7,CentOS8安装Elasticsearch6.8.6
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Docker快速安装Oracle11G,搭建oracle11g学习环境