亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python FTP操作類代碼分享

 更新時間:2014年05月13日 10:20:43   作者:  
這篇文章主要介紹了Python FTP操作類,實現(xiàn)自動下載、自動上傳,并可以遞歸目錄操作,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

#!/usr/bin/py2
# -*- coding: utf-8 -*-
#encoding=utf-8

'''''
    ftp自動下載、自動上傳腳本,可以遞歸目錄操作
''' 

from ftplib import FTP
import os, sys, string, datetime, time
import socket  

class FtpClient:

    def __init__(self, host, user, passwd, remotedir, port=21):
        self.hostaddr = host
        self.username = user
        self.password = passwd
        self.remotedir  = remotedir          
        self.port     = port
        self.ftp      = FTP()
        self.file_list = []  

    def __del__(self):
        self.ftp.close()  

    def login(self):
        ftp = self.ftp
        try:
            timeout = 60
            socket.setdefaulttimeout(timeout)
            ftp.set_pasv(True)
            ftp.connect(self.hostaddr, self.port)
            print 'Connect Success %s' %(self.hostaddr)
            ftp.login(self.username, self.password)
            print 'Login Success %s' %(self.hostaddr)
            debug_print(ftp.getwelcome())
        except Exception:
            deal_error("Connect Error or Login Error")
        try:
            ftp.cwd(self.remotedir)
        except(Exception):
            deal_error('Change Directory Error')  

    def is_same_size(self, localfile, remotefile):
        try:
            remotefile_size = self.ftp.size(remotefile)
        except:
            remotefile_size = -1
        try:
            localfile_size = os.path.getsize(localfile)
        except:
            localfile_size = -1
        debug_print('lo:%d  re:%d' %(localfile_size, remotefile_size),)
        if remotefile_size == localfile_size:
            return 1
        else:
            return 0

    def download_file(self, localfile, remotefile):
        if self.is_same_size(localfile, remotefile):
            return
        else:
            pass
        file_handler = open(localfile, 'wb')
        self.ftp.retrbinary('RETR %s'%(remotefile), file_handler.write)
        file_handler.close()

    def download_files(self, localdir='./', remotedir='./'):
        try:
            self.ftp.cwd(remotedir)
        except:
            return
        if not os.path.isdir(localdir):
            os.makedirs(localdir)
        self.file_list = []
        self.ftp.dir(self.get_file_list)
        remotenames = self.file_list
        for item in remotenames:
            filetype = item[0]
            filename = item[1]
            local = os.path.join(localdir, filename)
            if filetype == 'd':
                self.download_files(local, filename)
            elif filetype == '-':
                self.download_file(local, filename)
        self.ftp.cwd('..')  

    def upload_file(self, localfile, remotefile):
        if not os.path.isfile(localfile):
            return
        if self.is_same_size(localfile, remotefile):
            return
        file_handler = open(localfile, 'rb')
        self.ftp.storbinary('STOR %s' %remotefile, file_handler)
        file_handler.close()  

    def upload_files(self, localdir='./', remotedir = './'):
        if not os.path.isdir(localdir):
            return
        localnames = os.listdir(localdir)
        self.ftp.cwd(remotedir)
        for item in localnames:
            src = os.path.join(localdir, item)
            if os.path.isdir(src):
                try:
                    self.ftp.mkd(item)
                except:
                    debug_print('Directory Exists %s' %item)
                self.upload_files(src, item)
            else:
                self.upload_file(src, item)
        self.ftp.cwd('..')

    def mkdir(self, remotedir='./'):
        try:
            self.ftp.mkd(remotedir)
        except:
            debug_print('Directory Exists %s' %remotedir)

    def get_file_list(self, line):
        ret_arr = []
        file_arr = self.get_filename(line)
        if file_arr[1] not in ['.', '..']:
            self.file_list.append(file_arr)

    def get_filename(self, line):
        pos = line.rfind(':')
        while(line[pos] != ' '):
            pos += 1
        while(line[pos] == ' '):
            pos += 1
        file_arr = [line[0], line[pos:]]
        return file_arr

def debug_print(str):
    print (str)

def deal_error(e):
    timenow  = time.localtime()
    datenow  = time.strftime('%Y-%m-%d', timenow)
    logstr = '%s Error: %s' %(datenow, e)
    debug_print(logstr)
    file.write(logstr)
    sys.exit()

相關(guān)文章

  • 記錄模型訓(xùn)練時loss值的變化情況

    記錄模型訓(xùn)練時loss值的變化情況

    這篇文章主要介紹了記錄模型訓(xùn)練時loss值的變化情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 對Python函數(shù)設(shè)計規(guī)范詳解

    對Python函數(shù)設(shè)計規(guī)范詳解

    今天小編就為大家分享一篇對Python函數(shù)設(shè)計規(guī)范詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python編程對列表中字典元素進行排序的方法詳解

    Python編程對列表中字典元素進行排序的方法詳解

    這篇文章主要介紹了Python編程對列表中字典元素進行排序的方法,涉及Python針對列表及字典元素的遍歷、讀取、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • pyecharts的Tab和Legend布局詳情

    pyecharts的Tab和Legend布局詳情

    這篇文章主要介紹了pyecharts的Tab和Legend布局,pyecharts是百度開源的一款第三方繪圖模塊,結(jié)合的python語言的簡易性和Echarts的強大繪圖特性,可以用python對其調(diào)用,輸出交互性好,精美乖巧且符合審美的圖表,下文我們就來學(xué)習(xí)pyecharts的Tab和Legend煩人布局布局
    2022-03-03
  • python中matplotlib實現(xiàn)隨鼠標(biāo)滑動自動標(biāo)注代碼

    python中matplotlib實現(xiàn)隨鼠標(biāo)滑動自動標(biāo)注代碼

    這篇文章主要介紹了python中matplotlib實現(xiàn)隨鼠標(biāo)滑動自動標(biāo)注代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python正向最大匹配分詞和逆向最大匹配分詞的實例

    python正向最大匹配分詞和逆向最大匹配分詞的實例

    今天小編就為大家分享一篇python正向最大匹配分詞和逆向最大匹配分詞的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python如何將繪制的圖片保存為矢量圖格式(svg)

    python如何將繪制的圖片保存為矢量圖格式(svg)

    這篇文章主要介紹了python如何將繪制的圖片保存為矢量圖格式(svg)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 簡單易懂的python環(huán)境安裝教程

    簡單易懂的python環(huán)境安裝教程

    這篇文章主要為大家詳細(xì)介紹了簡單易懂的python環(huán)境安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • python必學(xué)知識之文件操作(建議收藏)

    python必學(xué)知識之文件操作(建議收藏)

    python中對文件、文件夾(文件操作函數(shù))的操作需要涉及到os模塊和shutil模塊。下面這篇文章主要給大家介紹了關(guān)于python必學(xué)知識之文件操作的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • python3利用smtplib通過qq郵箱發(fā)送郵件方法示例

    python3利用smtplib通過qq郵箱發(fā)送郵件方法示例

    python實現(xiàn)郵件發(fā)送較為簡單,主要用到smtplib這個模塊,所以下面這篇文章主要給大家介紹了關(guān)于python3利用smtplib通過qq郵箱發(fā)送郵件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。
    2017-12-12

最新評論