首页 文章 精选 留言 我的

精选列表

搜索[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

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

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

优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册