最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • python創(chuàng)建列表并給列表賦初始值的方法

    python創(chuàng)建列表并給列表賦初始值的方法

    這篇文章主要介紹了python創(chuàng)建列表并給列表賦初始值的方法,涉及Python列表的定義與賦值技巧,需要的朋友可以參考下
    2015-07-07
  • python中使用xlrd讀excel使用xlwt寫excel的實例代碼

    python中使用xlrd讀excel使用xlwt寫excel的實例代碼

    這篇文章主要介紹了python中使用xlrd讀excel使用xlwt寫excel的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python中下劃線含義詳解

    Python中下劃線含義詳解

    大家好,本篇文章主要講的是Python中下劃線含義詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • python使用JSON模塊進(jìn)行數(shù)據(jù)處理(編碼解碼)

    python使用JSON模塊進(jìn)行數(shù)據(jù)處理(編碼解碼)

    這篇文章主要為大家介紹了python使用JSON模塊進(jìn)行數(shù)據(jù)處理編碼解碼的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Python3 實現(xiàn)串口兩進(jìn)程同時讀寫

    Python3 實現(xiàn)串口兩進(jìn)程同時讀寫

    今天小編就為大家分享一篇Python3 實現(xiàn)串口兩進(jìn)程同時讀寫,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python運行時強制刷新緩沖區(qū)的方法

    python運行時強制刷新緩沖區(qū)的方法

    今天小編就為大家分享一篇python運行時強制刷新緩沖區(qū)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python實現(xiàn)將Unicode轉(zhuǎn)換為ASCII

    Python實現(xiàn)將Unicode轉(zhuǎn)換為ASCII

    這篇文章主要為大家詳細(xì)介紹了系統(tǒng)編碼的不同方法以及如何利用Python實現(xiàn)將Unicode轉(zhuǎn)換為?ASCII,文中的示例代碼講解詳細(xì),有需要的小伙伴可以學(xué)習(xí)一下
    2023-10-10
  • python-視頻分幀&多幀合成視頻實例

    python-視頻分幀&多幀合成視頻實例

    今天小編就為大家分享一篇python-視頻分幀&多幀合成視頻實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • PyTorch與PyTorch?Geometric的安裝過程

    PyTorch與PyTorch?Geometric的安裝過程

    這篇文章主要介紹了PyTorch與PyTorch?Geometric的安裝,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法詳解

    Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法詳解

    python中數(shù)據(jù)處理是比較方便的,經(jīng)常用的就是讀寫文件,提取數(shù)據(jù)等,本文主要介紹其中的一些用法,這篇文章主要給大家介紹了關(guān)于Python進(jìn)階學(xué)習(xí)之pandas中read_csv()用法的相關(guān)資料,需要的朋友可以參考下
    2024-03-03

最新評論

乡城县| 杨浦区| 霍林郭勒市| 报价| 贵港市| 竹溪县| 梅河口市| 定西市| 曲周县| 永泰县| 黎平县| 林西县| 美姑县| 天峻县| 黎川县| 璧山县| 合江县| 察雅县| 石嘴山市| 炉霍县| 泸水县| 庆城县| 新源县| 佛坪县| 姚安县| 广南县| 天等县| 郓城县| 荥经县| 宣汉县| 全南县| 河北省| 雷山县| 江安县| 海阳市| 历史| 罗甸县| 淮北市| 连城县| 贵定县| 阜城县|