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结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

python uv包管理小结

《pythonuv包管理小结》uv是一个高性能的Python包管理工具,它不仅能够高效地处理包管理和依赖解析,还提供了对Python版本管理的支持,本文主要介绍了pythonuv包管理小结,具有一... 目录安装 uv使用 uv 管理 python 版本安装指定版本的 Python查看已安装的 Python