本文主要是介绍python 一键Ftp 支持多机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#coding:utf-8
#__author__:swg
import paramiko,os,sys,json
current_file = sys.argv[0] if len(sys.argv)==1 else sys.argv[0]#配置例子、丢失配置文件的时候帮助用户添加,在当前目录下存在sftp-config.json即可
config_demo = '''
{"mkdir": "True","ignoreDir": [".git",".svn",".idea"],"base_dir" : "D:\\www\\app1\\","upload_app:"www.app1.com","uplpad_forder":["path1","path2\api2"],"remote_config" = {"Test-Host-A":{"hostname":'192.168.1.88',"username":'root',"password":'123456',"port":22,"path":"/home/www/games/"},"Test-Host-B" :{"hostname":'192.168.1.89',"username":'root',"password":'123456',"port":22,"path":"/home/www/web/"}}
}
'''#config reader
config_path = os.path.join(os.path.dirname(current_file),"sftp-config.json")
if not os.path.exists(config_path):print "\n\n config file(sftp-config.json) not found, \n\nif you lost,creeat it like this: \n\n %s \n\n " % config_demosys.exit()config_content = file(config_path)
config = json.load(config_content)#强制创建目录
mkdir = config['mkdir']
#忽略目录
ignoreDir = config['ignoreDir']#本地项目根目录
base_dir = config['base_dir']#需要上传的APP
upload_app = config['upload_app']#需要上传的目录
uplpad_forder = config['upload_forder']#远程配置
remote_config = config['remote_config']connect_pool = {}
for name,host in remote_config.items():try:sftp=paramiko.Transport((host['hostname'],host['port']))sftp.connect(username=host['username'],password=host['password'])sftp = paramiko.SFTPClient.from_transport(sftp)ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host['hostname'], host['port'], host['username'], host['password']) connect_pool[name] = {'sftp':sftp,'ssh':ssh,'path':host['path'],'host':host['hostname'],}except:raise "can not connect %s (%s)" % (name,host['hostname']) print "connect %s (%s) ok" % (name,host['hostname'])def UploadToRemote(files):global connect_pool,base_dirfor name,host in connect_pool.items():local_file = (base_dir+'\\'+files).replace('/',"\\")remote_file = (host['path'] +"/"+files).replace("///",'/') print "%s ==> %s:%s " % (local_file.replace('\\\\\\','\\') ,host['host'],remote_file) ,try:host['sftp'].put(local_file,remote_file)print ' ok'except Exception ,e:print ' fail',edef MkRemoteDir(dirs):global connect_poolfor name,host in connect_pool.items():remote_dir = (host['path'] +dirs).replace('//','/')print "mkdir: " + remote_dir,try:# stdin, stdout, stderr = host['ssh'].exec_command('mkdir -p %s' % remote_dir)host['ssh'].exec_command('mkdir -p %s' % remote_dir)print "ok"except Exception ,e:print ' Fail! 'for local_dir in uplpad_forder:for root,dirs,files in os.walk(base_dir +"\\"+ upload_app+"\\"+local_dir):for filespath in files:local_file = os.path.join(root,filespath)local_file = local_file.replace(base_dir,'')remote_file = local_file.replace("\\","/")passFile = Falsefor f in ignoreDir:if remote_file.find(f)!=-1:print remote_file ,"============> ignore"passFile = Truebreak;if not passFile:UploadToRemote(remote_file)if mkdir == True:for dirname in dirs:passFile = Falsefull_dir = os.path.join(root,dirname)for f in ignoreDir:if full_dir.find(f)!=-1:# print full_dir ,"============> ignore"passFile = Truebreak;if not passFile:dir_name = os.path.join(root,dirname)MkRemoteDir( dir_name.replace(base_dir,'').replace("\\","/") )else:passprint "\n\nDone"if sys.platform[:3] == 'win':os.system("pause")
这篇关于python 一键Ftp 支持多机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!