Python實現(xiàn)FTP上傳文件或文件夾實例(遞歸)
本文實例講述了Python實現(xiàn)FTP上傳文件或文件夾實例。分享給大家供大家參考。具體如下:
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)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
PyCharm調(diào)用matplotlib繪圖時圖像彈出問題詳解
這篇文章主要給大家介紹了關于PyCharm調(diào)用matplotlib繪圖時圖像彈出問題的相關資料,文中通過圖文介紹的非常詳細,對大家學習或者使用PyCharm具有一定的參考學習價值,需要的朋友可以參考下2022-07-07
python 監(jiān)聽salt job狀態(tài),并任務數(shù)據(jù)推送到redis中的方法
今天小編就為大家分享一篇python 監(jiān)聽salt job狀態(tài),并任務數(shù)據(jù)推送到redis中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
PyCharm中New Directory 和 New Python
python package這是一個特殊的目錄,因為在創(chuàng)建該python package的時候,系統(tǒng)會自動地生成一個py文件, init.py,這篇文章主要介紹了PyCharm中New Directory 和 New Python Package的區(qū)別,需要的朋友可以參考下2023-12-12
Python3中configparser模塊讀寫ini文件并解析配置的用法詳解
這篇文章主要介紹了Python3中configparser模塊讀寫ini文件并解析配置的用法詳解,需要的朋友可以參考下2020-02-02
Python中字符串,列表與字典的常用拼接方法總結(jié)
有時在數(shù)據(jù)處理時,需要對數(shù)據(jù)進行拼接處理,比如字符串的拼接、列表的拼接等,本文主要是介紹了字符串、列表、字典常用的拼接方法,希望對大家有所幫助2024-02-02
結(jié)合Python的SimpleHTTPServer源碼來解析socket通信
SimpleHTTPServer是Python中一個現(xiàn)成的HTTP服務器例子,本文我們將結(jié)合Python的SimpleHTTPServer源碼來解析socket通信,我們先來看一下socket的基本概念:2016-06-06
Python實現(xiàn)將HTML轉(zhuǎn)為PDF/圖片/XML/XPS格式
網(wǎng)頁內(nèi)容是信息傳播的主要形式之一,這篇文章主要和大家介紹了如何使用Python實現(xiàn)將HTML分別轉(zhuǎn)為PDF/圖片/XML/XPS格式等,需要的可以參考下2024-03-03

