python 一鍵Ftp 支援多機

2021-07-24 20:29:35 字數 3449 閱讀 1510

#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 = '''

, "test-host-b" : }}

'''#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_demo

sys.exit()

config_content = file(config_path)

config = json.load(config_content)

#強制建立目錄

mkdir = config['mkdir']

#忽略目錄

ignoredir = config['ignoredir']

#本地專案根目錄

base_dir = config['base_dir']

#需要上傳的目錄

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] =

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_dir

for 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',e

def mkremotedir(dirs):

global connect_pool

for 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 filespath in files:

local_file = os.path.join(root,filespath)

local_file = local_file.replace(base_dir,'')

remote_file = local_file.replace("\\","/")

passfile = false

for f in ignoredir:

if remote_file.find(f)!=-1:

print remote_file ,"**********==> ignore"

passfile = true

break;

if not passfile:

uploadtoremote(remote_file)

if mkdir == true:

for dirname in dirs:

passfile = false

full_dir = os.path.join(root,dirname)

for f in ignoredir:

if full_dir.find(f)!=-1:

# print full_dir ,"**********==> ignore"

passfile = true

break;

if not passfile:

dir_name = os.path.join(root,dirname)

mkremotedir( dir_name.replace(base_dir,'').replace("\\","/") )

else:

pass

print "\n\ndone"

if sys.platform[:3] == 'win':

os.system("pause")

Python一鍵多值

import csv file path f jddata classify300.csv csvfile file file path,rb reader csv.reader csvfile 初始化字典 adict dict 遍歷csv檔案裡的每一行資料,其中imagename為資料的第一列,c...

設定一鍵啟動多檔案

我們電腦裡有很多檔案,對於每天都需要的檔案,或者對於類似的檔案,可能同一時間內需要開啟多次,可不可以達到設定一鍵啟動多檔案的效果呢,這裡分享一下 比如 我們 d 盤有乙個檔案叫1.txt 我們 e 盤有乙個 2.html檔案 那麼我怎麼能同時一鍵啟動這兩個檔案呢,首先,我們再桌面 當然可以是任何位置...

Python 字典的一鍵多值,即乙個鍵對應多個值

encoding utf 8 print 中國 字典的一鍵多值 print 方案一 list作為dict的值 值允許重複 d1 key 1 value 2 value 2 print d1 獲取值 print 方案一 獲取值 print list d1 key print 方案一 刪除值,會留下乙個...