python實(shí)現(xiàn)FTP循環(huán)上傳文件
本文實(shí)例為大家分享了python實(shí)現(xiàn)FTP循環(huán)上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
測(cè)試過程中,有時(shí)會(huì)用到FTP的數(shù)據(jù)流,或者需要使用FTP反復(fù)上傳文件,所以寫了一個(gè)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服務(wù)器
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服務(wù)器: %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服務(wù)器: %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)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
json 轉(zhuǎn) mot17數(shù)據(jù)格式的實(shí)現(xiàn)代碼 (親測(cè)有效)
這篇文章主要介紹了json 轉(zhuǎn) mot17數(shù)據(jù)格式的實(shí)現(xiàn)代碼 (親測(cè)有效),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python梯度提升庫XGBoost解決機(jī)器學(xué)習(xí)問題使用探究
XGBoost是一個(gè)流行的梯度提升庫,特別適用于解決各種機(jī)器學(xué)習(xí)問題,它在性能和速度上表現(xiàn)出色,常被用于分類、回歸、排序、推薦系統(tǒng)等應(yīng)用,本文將介紹XGBoost的基本原理、核心功能以及一些詳細(xì)的示例代碼2024-01-01
配置python的編程環(huán)境之Anaconda + VSCode的教程
這篇文章主要介紹了配置python的編程環(huán)境之Anaconda + VSCode的教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Python實(shí)現(xiàn)各種排序算法的代碼示例總結(jié)
這篇文章主要介紹了Python實(shí)現(xiàn)各種排序算法的代碼示例總結(jié),其實(shí)Python是非常好的算法入門學(xué)習(xí)時(shí)的配套高級(jí)語言,需要的朋友可以參考下2015-12-12
Python標(biāo)準(zhǔn)庫中的logging用法示例詳解
logging是Python標(biāo)準(zhǔn)庫中記錄常用的記錄日志庫,通過logging模塊存儲(chǔ)各種格式的日志,主要用于輸出運(yùn)行日志,可以設(shè)置輸出日志的等級(jí)、日志保存路徑、日志文件回滾等,這篇文章主要介紹了Python標(biāo)準(zhǔn)庫中的logging,需要的朋友可以參考下2022-09-09
Python分析特征數(shù)據(jù)類別與預(yù)處理方法速學(xué)
這篇文章主要為大家介紹了Python分析特征數(shù)據(jù)類別與預(yù)處理方法速學(xué),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
使用python讀取.text文件特定行的數(shù)據(jù)方法
今天小編就為大家分享一篇使用python讀取.text文件特定行的數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01

