Scenario Description:
1. The macfee antivirus software will update the patch package regularly, which needs to be downloaded on the official website, and the customer will manually update the patch package;
2. The patch packs need to be cleaned regularly so as not to occupy too much disk space.
3. Download log records are required.
Procedure function:
1. Use urllib2 to grab data from the website and download it to the specified path;
2. To avoid repeated downloading, make data comparison before downloading;
3. Use multithreading, one for downloading and the other for cleaning;
4. Once every 24 hours.
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