首页 文章 精选 留言 我的

精选列表

搜索[Python],共10000篇文章
优秀的个人博客,低调大师

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

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册