Python實(shí)現(xiàn)FTP上傳文件或文件夾實(shí)例(遞歸)
本文實(shí)例講述了Python實(shí)現(xiàn)FTP上傳文件或文件夾實(shí)例。分享給大家供大家參考。具體如下:
import sys
import os
import json
from ftplib import FTP
_XFER_FILE = 'FILE'
_XFER_DIR = 'DIR'
class Xfer(object):
'''''
@note: upload local file or dirs recursively to ftp server
'''
def __init__(self):
self.ftp = None
def __del__(self):
pass
def setFtpParams(self, ip, uname, pwd, port = 21, timeout = 60):
self.ip = ip
self.uname = uname
self.pwd = pwd
self.port = port
self.timeout = timeout
def initEnv(self):
if self.ftp is None:
self.ftp = FTP()
print '### connect ftp server: %s ...'%self.ip
self.ftp.connect(self.ip, self.port, self.timeout)
self.ftp.login(self.uname, self.pwd)
print self.ftp.getwelcome()
def clearEnv(self):
if self.ftp:
self.ftp.close()
print '### disconnect ftp server: %s!'%self.ip
self.ftp = None
def uploadDir(self, localdir='./', remotedir='./'):
if not os.path.isdir(localdir):
return
self.ftp.cwd(remotedir)
for file in os.listdir(localdir):
src = os.path.join(localdir, file)
if os.path.isfile(src):
self.uploadFile(src, file)
elif os.path.isdir(src):
try:
self.ftp.mkd(file)
except:
sys.stderr.write('the dir is exists %s'%file)
self.uploadDir(src, file)
self.ftp.cwd('..')
def uploadFile(self, localpath, remotepath='./'):
if not os.path.isfile(localpath):
return
print '+++ upload %s to %s:%s'%(localpath, self.ip, remotepath)
self.ftp.storbinary('STOR ' + remotepath, open(localpath, 'rb'))
def __filetype(self, src):
if os.path.isfile(src):
index = src.rfind('\\')
if index == -1:
index = src.rfind('/')
return _XFER_FILE, src[index+1:]
elif os.path.isdir(src):
return _XFER_DIR, ''
def upload(self, src):
filetype, filename = self.__filetype(src)
self.initEnv()
if filetype == _XFER_DIR:
self.srcDir = src
self.uploadDir(self.srcDir)
elif filetype == _XFER_FILE:
self.uploadFile(src, filename)
self.clearEnv()
if __name__ == '__main__':
srcDir = r"C:\sytst"
srcFile = r'C:\sytst\sar.c'
xfer = Xfer()
xfer.setFtpParams('192.x.x.x', 'jenkins', 'pass')
xfer.upload(srcDir)
xfer.upload(srcFile)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬取百度翻譯實(shí)現(xiàn)中英互譯功能
這篇文章主要介紹了利用Python爬蟲(chóng)爬取百度翻譯,從而實(shí)現(xiàn)中英文互譯的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-01-01
基于Python實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別系統(tǒng)
這篇文章主要介紹了如何通過(guò)Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的人臉識(shí)別系統(tǒng),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的可以跟隨小編一起試一試2022-01-01
使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù)
這篇文章主要介紹了使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
python的pdb調(diào)試命令的命令整理及實(shí)例
這篇文章主要介紹了python的pdb調(diào)試命令的命令整理及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07
python接口自動(dòng)化之正則用例參數(shù)化的示例詳解
這篇文章主要介紹了python接口自動(dòng)化之正則用例參數(shù)化,它是一個(gè)特殊的字符序列,它能幫助你方便的檢查一個(gè)字符串是否與某種模式匹配,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Python最好的日期處理庫(kù)pendulum的使用指南
關(guān)于日期處理,Python?提供了很多的庫(kù),比如標(biāo)準(zhǔn)庫(kù)?datetime、第三方庫(kù)?dateutil、arrow?等等。本文將為大家介紹一個(gè)小編最喜歡的庫(kù)?pendulum,用起來(lái)可以說(shuō)非常的方便,任何對(duì)日期的操作它都能滿(mǎn)足2022-07-07

