更新服务器文件到本地

#!/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[line:%(lineno)d] %(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['state']
if state=='200':
dataList = jsoObj['data']
if dataList is not None:
for data in dataList:
#time.sleep(1)
#log.info(data)
js = json.loads(data)
filename= js['filename']
servicepath= js['servicepath']
clientpath= js['clientpath']
md5=js['md5']
downLoadFile(servicepath,clientpath,md5)

else:
log.info('no find the sync file,please check the magent_config table..................')
else:
log.info(jsoObj['message'])

'''
根据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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值