python實現(xiàn)FTP循環(huán)上傳文件
更新時間:2020年03月20日 09:33:52 作者:阿鵬2019
這篇文章主要為大家詳細介紹了python實現(xiàn)FTP循環(huán)上傳文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python實現(xiàn)FTP循環(huán)上傳文件的具體代碼,供大家參考,具體內容如下
測試過程中,有時會用到FTP的數(shù)據(jù)流,或者需要使用FTP反復上傳文件,所以寫了一個FTP循環(huán)上傳文件的python代碼。
代碼如下:
#coding=utf-8
import sys
import os
from ftplib import FTP
from time import sleep
_XFER_FILE = 'FILE'
_XFER_DIR = 'DIR'
class Transmitter(object): # 注意:遞歸上傳本地文件或dirs到ftp服務器
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('### 連接FTP服務器: %s ...' % self.ip)
self.ftp.connect(self.ip, self.port, self.timeout)
self.ftp.login(self.uname, self.pwd)
def clearEnv(self):
if self.ftp:
self.ftp.close()
print('### 斷開FTP服務器: %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('目錄存在 %s' % file)
self.uploadDir(src, file)
self.ftp.cwd('..')
def uploadFile(self, localpath, remotepath='./'):
if not os.path.isfile(localpath):
return
print('+++ 上傳 %s to %s:%s' % (localpath, self.ip, remotepath))
self.ftp.storbinary('STOR ' + remotepath, open(localpath, 'rb'))
sleep(0.5)
try:
self.ftp.delete(remotepath)
except:
pass
# del file when uploaded this file
# os.remove(localpath)
# sleep(1)
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:\Users\Administrator\Downloads\FTP\smp'
transmitter = Transmitter()
transmitter.setFtpParams('10.44.0.2', 'admin', '123123')
while True:
transmitter.upload(srcDir)
sleep(4)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
json 轉 mot17數(shù)據(jù)格式的實現(xiàn)代碼 (親測有效)
這篇文章主要介紹了json 轉 mot17數(shù)據(jù)格式的實現(xiàn)代碼 (親測有效),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Python梯度提升庫XGBoost解決機器學習問題使用探究
XGBoost是一個流行的梯度提升庫,特別適用于解決各種機器學習問題,它在性能和速度上表現(xiàn)出色,常被用于分類、回歸、排序、推薦系統(tǒng)等應用,本文將介紹XGBoost的基本原理、核心功能以及一些詳細的示例代碼2024-01-01
配置python的編程環(huán)境之Anaconda + VSCode的教程
這篇文章主要介紹了配置python的編程環(huán)境之Anaconda + VSCode的教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Python分析特征數(shù)據(jù)類別與預處理方法速學
這篇文章主要為大家介紹了Python分析特征數(shù)據(jù)類別與預處理方法速學,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
使用python讀取.text文件特定行的數(shù)據(jù)方法
今天小編就為大家分享一篇使用python讀取.text文件特定行的數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

