python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例
網(wǎng)上大部分都是上傳文件,于是個(gè)人參照網(wǎng)上一些博客的內(nèi)容,寫(xiě)了一個(gè)把windows上目錄上傳到遠(yuǎn)程linux的一個(gè)小程序。
下面是代碼:
class ExportPrepare(object):
def __init__(self):
pass
def sftp_con(self):
t = paramiko.Transport((self.ip, self.port))
t.connect(username=self.username, password=self.password)
return t
# 找到所有你要上傳的目錄已經(jīng)文件。
def __get_all_files_in_local_dir(self, local_dir):
all_files = list()
if os.path.exists(local_dir):
files = os.listdir(local_dir)
for x in files:
filename = os.path.join(local_dir, x)
print "filename:" + filename
# isdir
if os.path.isdir(filename):
all_files.extend(self.__get_all_files_in_local_dir(filename))
else:
all_files.append(filename)
else:
print '{}does not exist'.format(local_dir)
return all_files
# Copy a local file (localpath) to the SFTP server as remotepath
def sftp_put_dir(self):
try:
#本地test目錄上傳到遠(yuǎn)程root/usr/下面
local_dir = "c:/test"
remote_dir = "/root/usr/test"
t = self.sftp_con()
sftp = paramiko.SFTPClient.from_transport(t)
# sshclient
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ip, port=self.port, username=self.username, password=self.password, compress=True)
ssh.exec_command('rm -rf ' + remote_dir)
if remote_dir[-1] == '/':
remote_dir = remote_dir[0:-1]
all_files = self.__get_all_files_in_local_dir(local_dir)
for x in all_files:
filename = os.path.split(x)[-1]
remote_file = os.path.split(x)[0].replace(local_dir, remote_dir)
path = remote_file.replace('\\', '/')
# 創(chuàng)建目錄 sftp的mkdir也可以,但是不能創(chuàng)建多級(jí)目錄所以改用ssh創(chuàng)建。
tdin, stdout, stderr = ssh.exec_command('mkdir -p ' + path)
print stderr.read()
remote_filename = path + '/' + filename
print u'Put files...' + filename
sftp.put(x, remote_filename)
ssh.close()
except Exception, e:
print e
if __name__=='__main__':
export_prepare = ExportPrepare()
export_prepare.sftp_put_dir()
比較匆忙,不足之處可以指出,共同進(jìn)步。
以上這篇python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python辦公自動(dòng)化從Excel中計(jì)算整理數(shù)據(jù)并寫(xiě)入Word
- Python辦公自動(dòng)化Word轉(zhuǎn)Excel文件批量處理
- Python辦公自動(dòng)化批量處理文件實(shí)現(xiàn)示例
- Python自動(dòng)化辦公之手機(jī)號(hào)提取
- Python自動(dòng)化辦公之Word文件內(nèi)容的讀取
- Python使用sftp實(shí)現(xiàn)傳文件夾和文件
- Python使用sftp實(shí)現(xiàn)上傳和下載功能
- python實(shí)現(xiàn)自動(dòng)下載sftp文件
- 基于python實(shí)現(xiàn)FTP文件上傳與下載操作(ftp&sftp協(xié)議)
- Python辦公自動(dòng)化SFTP詳解
相關(guān)文章
基于Python實(shí)現(xiàn)Word轉(zhuǎn)HTML
將Word轉(zhuǎn)換為HTML能將文檔內(nèi)容發(fā)布在網(wǎng)頁(yè)上,這樣,用戶就可以通過(guò)瀏覽器直接查看或閱讀文檔而無(wú)需安裝特定的軟件,下面我們就來(lái)學(xué)習(xí)一下Python是如何實(shí)現(xiàn)Word轉(zhuǎn)HTML的吧2023-12-12
Python3將數(shù)據(jù)保存為txt文件的方法
這篇文章主要介紹了Python3將數(shù)據(jù)保存為txt文件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
解決Tensorflow安裝成功,但在導(dǎo)入時(shí)報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決Tensorflow安裝成功,但在導(dǎo)入時(shí)報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
總結(jié)Python編程中函數(shù)的使用要點(diǎn)
這篇文章主要介紹了Python編程中函數(shù)的使用要點(diǎn)總結(jié),文中也講到了人民群眾喜聞樂(lè)見(jiàn)的lambda表達(dá)式的用法,需要的朋友可以參考下2016-03-03
MoviePy簡(jiǎn)介及Python視頻剪輯自動(dòng)化
MoviePy是一個(gè)用于視頻編輯的Python模塊,可用于基本操作(如剪切、拼接、字幕插入)、視頻合成、視頻處理或創(chuàng)建高級(jí)效果等。本文給大家介紹MoviePy簡(jiǎn)介及Python視頻剪輯自動(dòng)化的相關(guān)知識(shí),感興趣的朋友一起看看吧2020-12-12
python?Ajenti控制面板輕松地管理所有服務(wù)器網(wǎng)站
Ajenti是一個(gè)值得擁有的管理面板,免費(fèi)開(kāi)源的管理面板工具,可以幫助你集中管理多個(gè)服務(wù)器和網(wǎng)站,Ajenti?支持?Linux、BSD、Mac?OS?X和Windows?等多個(gè)操作系統(tǒng),并且可以通過(guò)一個(gè)直觀的?Web?界面來(lái)完成各種系統(tǒng)管理任務(wù)2024-01-01
Python實(shí)現(xiàn)批量將word轉(zhuǎn)換成pdf
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)批量將word文檔轉(zhuǎn)換成pdf文件,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08
python通用數(shù)據(jù)庫(kù)操作工具 pydbclib的使用簡(jiǎn)介
這篇文章主要介紹了python通用數(shù)據(jù)庫(kù)操作工具 pydbclib的使用簡(jiǎn)介,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
pandas pd.cut()與pd.qcut()的具體實(shí)現(xiàn)
本文主要介紹了pandas pd.cut()與pd.qcut()的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

