• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python FTP操作类代码分享

Python FTP操作类代码分享

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python ftp,python ftp下载,python ftp.nlst,python ftp上传,python ftp模块等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

'''''
    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)
  &n

您可能想查找下面的文章:

  • python实现FTP服务器服务的方法
  • python实现FTP服务器服务的方法
  • Python中的FTP通信模块ftplib的用法整理
  • Python中的FTP通信模块ftplib的用法整理
  • Python ftp上传文件
  • python实现的简单FTP上传下载文件实例
  • python实现简单ftp客户端的方法
  • python实现支持目录FTP上传下载文件的方法
  • Python使用ftplib实现简易FTP客户端的方法
  • python通过ftplib登录到ftp服务器的方法

相关文章

  • python中遍历文件的3个方法
  • 让Python代码更快运行的5种方法
  • Python使用urllib2获取网络资源实例讲解
  • 详解Python中类的定义与使用
  • python sort、sorted高级排序技巧
  • Python设计模式之单例模式实例
  • Python中MYSQLdb出现乱码的解决方法
  • python机器学习库xgboost的使用
  • 从局部变量和全局变量开始全面解析Python中变量的作用域
  • Python实现Windows上气泡提醒效果的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python开发WebService系列教程之REST,web.py,eurasia,Django
    • 举例讲解Python程序与系统shell交互的方式
    • Python中的特殊语法:filter、map、reduce、lambda介绍
    • Python 序列化 pickle/cPickle模块使用介绍
    • Python脚本实时处理log文件的方法
    • python检查URL是否正常访问的小技巧
    • tensorflow查看ckpt各节点名称实例
    • Python虚拟环境Virtualenv使用教程
    • python绘图方法实例入门
    • Python变量作用范围实例分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有