更新服务器文件到本地
#!/usr/bin/python #-- coding: utf-8 -- #\magent\src\restservice\synchcofig.py ''' 用于同步服务器的配置文件 ''' import urllib2 #import tools import logging,logging.config import os import json import hashlib import thread import time import urllib from time import sleep import threading
FILE=os.getcwd()
LOG_NAME='syncSysConfigFile'
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(filename)s %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename = os.path.join(FILE,'log.txt'), filemode='w' ) console = logging.StreamHandler() console.setLevel(logging.DEBUG) formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') console.setFormatter(formatter) logging.getLogger(LOG_NAME).addHandler(console)
log = logging.getLogger(LOG_NAME)
SERVICE_IP='132.121.130.18:8008' threads= index=1
#生成本地的MD5文件,用于比对服务器上的MD5文件 def sendRequest(): threads= index=1 log.info('send request to service............') url = 'http://'+SERVICE_IP+'/checkFile/' jsonStr = urllib2.urlopen(url).read() jsoObj = json.loads(jsonStr) state = jsoObj if state=='200': dataList = jsoObj if dataList is not None: for data in dataList: #time.sleep(1) #log.info(data) js = json.loads(data) filename= js servicepath= js clientpath= js md5=js downLoadFile(servicepath,clientpath,md5)
else:
log.info('no find the sync file,please check the magent_config table..................')
else:
log.info(jsoObj)
''' 根据MD5值进行判断,如果不一致,则从新从服务器上下载文件 ''' def downLoadFile(servicepath,clientpath,md5): down = False if os.path.exists(clientpath): #如果存在,则判断md5值是否一致 newMD5 = MD5(clientpath) if newMD5 == md5: pass else: down = True
else:
dirname=os.path.dirname(clientpath)
if os.path.exists(dirname):
pass
else:
os.makedirs(dirname)
down = True
#启用线程进行下载
if down:
"""global index
index = index + 1"""
t = threading.Thread(target=mulitThreadStart,args=(servicepath,clientpath,1))
t.start()
threads.append(t)
for i in threads:
t.join()
#采用多线程进行下载 def mulitThreadStart(servicepath,clientpath,index): time.sleep(index) log.info(time.strftime('%Y-%m-%d %H:%M:%S') + ' ' +servicepath + '---->' +clientpath) url = 'http://'+SERVICE_IP+'/downloadFile2/?downpath=' +servicepath urllib.urlretrieve(url,clientpath)
#获取文件的md5值 def MD5(filepath): with open(filepath,'rb') as f: md5obj = hashlib.md5() md5obj.update(f.read()) hash = md5obj.hexdigest() return hash
if name == 'main': log.info(time.strftime('%Y-%m-%d %H:%M:%S') +' begin to synchronou service file.............') sendRequest() log.info(time.strftime('%Y-%m-%d %H:%M:%S') +' synchronou service file end.............')
---2016-05-24
@dianxinguangchang.zhongshanerlu.yuexiuqu.guangzhoushi.guangdongsheng