python 一键Ftp 支持多机

2024-03-01 09:18
文章标签 python 支持 一键 ftp 多机

本文主要是介绍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 支持多机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/761946

相关文章

利用Python实现添加或读取Excel公式

《利用Python实现添加或读取Excel公式》Excel公式是数据处理的核心工具,从简单的加减运算到复杂的逻辑判断,掌握基础语法是高效工作的起点,下面我们就来看看如何使用Python进行Excel公... 目录python Excel 库安装Python 在 Excel 中添加公式/函数Python 读取

Python实现合并与拆分多个PDF文档中的指定页

《Python实现合并与拆分多个PDF文档中的指定页》这篇文章主要为大家详细介绍了如何使用Python实现将多个PDF文档中的指定页合并生成新的PDF以及拆分PDF,感兴趣的小伙伴可以参考一下... 安装所需要的库pip install PyPDF2 -i https://pypi.tuna.tsingh

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

python中time模块的常用方法及应用详解

《python中time模块的常用方法及应用详解》在Python开发中,时间处理是绕不开的刚需场景,从性能计时到定时任务,从日志记录到数据同步,时间模块始终是开发者最得力的工具之一,本文将通过真实案例... 目录一、时间基石:time.time()典型场景:程序性能分析进阶技巧:结合上下文管理器实现自动计时

python中的整除向下取整的操作方法

《python中的整除向下取整的操作方法》Python中的//是整数除法运算符,用于执行向下取整的除法,返回商的整数部分,不会四舍五入,它在分治法、索引计算和整数运算中非常有用,本文给大家介绍pyth... 目录1. // 的基本用法2. // vs /(普通除法)3. // 在 mid = len(lis

基于Python和Tkinter实现高考倒计时功能

《基于Python和Tkinter实现高考倒计时功能》随着高考的临近,每个考生都在紧锣密鼓地复习,这时候,一款实用的倒计时软件能有效帮助你规划剩余时间,提醒你不要浪费每一分每一秒,今天,我们来聊聊一款... 目录一、软件概述:二、功能亮点:1. 高考倒计时2. 添加目标倒计时3. 励志语句4. 透明度调节与

Python如何查看数据的类型

《Python如何查看数据的类型》:本文主要介绍Python如何查看数据的类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python查看数据的类型1. 使用 type()2. 使用 isinstance()3. 检查对象的 __class__ 属性4.

Python实现PDF与多种图片格式之间互转(PNG, JPG, BMP, EMF, SVG)

《Python实现PDF与多种图片格式之间互转(PNG,JPG,BMP,EMF,SVG)》PDF和图片是我们日常生活和工作中常用的文件格式,有时候,我们可能需要将PDF和图片进行格式互转来满足... 目录一、介绍二、安装python库三、Python实现多种图片格式转PDF1、单张图片转换为PDF2、多张图

利用Python和C++解析gltf文件的示例详解

《利用Python和C++解析gltf文件的示例详解》gltf,全称是GLTransmissionFormat,是一种开放的3D文件格式,Python和C++是两个非常强大的工具,下面我们就来看看如何... 目录什么是gltf文件选择语言的原因安装必要的库解析gltf文件的步骤1. 读取gltf文件2. 提

Python容器类型之列表/字典/元组/集合方式

《Python容器类型之列表/字典/元组/集合方式》:本文主要介绍Python容器类型之列表/字典/元组/集合方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 列表(List) - 有序可变序列1.1 基本特性1.2 核心操作1.3 应用场景2. 字典(D