本站消息

站长简介/公众号


站长简介:高级软件工程师,曾在阿里云,每日优鲜从事全栈开发工作,利用周末时间开发出本站,欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-12(17)

2021-01(52)

python从ftp客户端读取文件

发布于2021-10-18 00:24     阅读(505)     评论(0)     点赞(5)     收藏(2)



废话就不说了,这里就给出我封装的 ftp 工具类,有兴趣的自己看一下吧–_--

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ftplib import FTP
from oslo_log import log

import hashlib
import os
import time


LOG = log.getLogger(__name__)

# 定义时间
sys_time = time.time()
sys_time_array = time.localtime(sys_time)
current_time = time.strftime("%Y-%m-%d %H:%M:%S:", sys_time_array)


class ftp(object):
    def __init__(self, ip, port, user, password):
        self.ip = ip
        self.port = port
        self.user = user
        self.password = password

    # FTP登录模块
    def ftp_login(self):
        ftp = FTP()
        try:
            ftp.connect(self.ip, self.port)
        except Exception:
            LOG.error("FTP connect failed!!!")
        try:
            ftp.login(self.user, self.password)
        except Exception:
            LOG.error("FTP connect failed!!!")
        else:
            return ftp

    # FTP下载
    def ftp_download(self, remote_path, local_path):
        ftp = FTP()
        try:
            ftp.connect(self.ip, self.port)
        except Exception:
            LOG.error("FTP connect failed!!!")
        try:
            ftp.login(self.user, self.password)
        except Exception:
            LOG.error("FTP login failed!!!")
        else:
            try:
                file_list = ftp.nlst(remote_path)
            except Exception:
                LOG.error("remote_path error!!!")
            else:
                key = os.path.exists(local_path)
                if str(key) == 'True':
                    pass
                else:
                    os.makedirs(local_path)
                try:
                    for file in file_list:
                        bufsize = 1024
                        file_name = file.split('/')[-1]
                        local_file = open(local_path + file_name, 'wb')
                        ftp.retrbinary('RETR %s' %
                                       (file), local_file.write, bufsize)
                        ftp.set_debuglevel(0)
                        local_file.close()
                except Exception:
                    LOG.error("%s %s download failed!!!" %
                              (current_time, remote_path))
                else:
                    LOG.error("%s %s download successfully!!!" %
                              (current_time, remote_path))

    # FTP下载
    def ftp_upload(self, remote_path, local_path):
        ftp = FTP()
        try:
            ftp.connect(self.ip, self.port)
        except Exception:
            LOG.error("FTP connect failed!!!")
        try:
            ftp.login(self.user, self.password)
        except Exception:
            LOG.error("FTP login failed!!!")
        else:
            try:
                ftp.mkd(remote_path)
            except Exception:
                pass
            try:
                file_list = os.walk(local_path)
                for root, dirs, files in file_list:
                    for file in files:
                        local_file = local_path + file
                        remote_file = remote_path + file
                        bufsize = 1024
                        fp = open(local_file, 'rb')
                        ftp.storbinary('STOR ' + remote_file, fp, bufsize)
                        fp.close()
            except Exception:
                LOG.error("%s %s upload failed!!!" %
                          (current_time, local_path))
            else:
                LOG.error("%s %s upload successfully!!!" %
                          (current_time, local_path))

    def remove_local_file(self, local_file_path):
        if os.path.exists(local_file_path):
            # 删除文件,可使用以下两种方法。
            os.remove(local_file_path)
        else:
            LOG.error('no such file:%s' % local_file_path.split('/')[-1])

    # FTP文件校验
    def check(self, remote_path, local_path):
        # 将FTP临时文件下载到本地临时目录
        tmp_path = '/tmp' + remote_path
        ftp = FTP()
        try:
            ftp.connect(self.ip, self.port)
        except Exception:
            LOG.error("FTP connect failed!!!")
        try:
            ftp.login(self.user, self.password)
        except Exception:
            LOG.error("FTP login failed!!!")
        else:
            try:
                file_list = ftp.nlst(remote_path)
            except Exception:
                LOG.error("remote_path error!!!")
            else:
                key = os.path.exists(tmp_path)
                if str(key) == 'True':
                    pass
                else:
                    os.makedirs(tmp_path)
                try:
                    for file in file_list:
                        bufsize = 1024
                        file_name = file.split('/')[-1]
                        local_file = open(tmp_path + file_name, 'wb')
                        ftp.retrbinary('RETR %s' %
                                       (file), local_file.write, bufsize)
                        ftp.set_debuglevel(0)
                        local_file.close()
                except Exception:
                    LOG.error("cloudn't check!!! \
                        it may be cause by your wrong FTP info!!!")
            # 校验FTP临时文件的md5值
            tmp_file_list = os.walk(tmp_path)
            tmp_file_md5s = {}
            for root, dirs, files in tmp_file_list:
                for tmp_file in files:
                    tmp_file_path = tmp_path + tmp_file
                    f_tmp = open(tmp_file_path, 'r')
                    fcont = f_tmp.read()
                    f_tmp.close()
                    fmd5 = hashlib.md5(fcont)
                    tmp_file_md5s[tmp_file] = fmd5.hexdigest()

            # 校验本地文件的md5值
            local_file_list = os.walk(local_path)
            local_file_md5s = {}
            for root, dirs, files in local_file_list:
                for local_file in files:
                    local_file_path = local_path + local_file
                    f_local = open(local_file_path, 'r')
                    fcont = f_local.read()
                    f_local.close()
                    fmd5 = hashlib.md5(fcont)
                    local_file_md5s[local_file] = fmd5.hexdigest()

            # 开始校验文件md5值是否相同
            for k, v in tmp_file_md5s.items():
                try:
                    if tmp_file_md5s[k] == local_file_md5s[k]:
                        pass
                    else:
                        ftp = FTP()
                        ftp.connect(self.ip, self.port)
                        ftp.login(self.user, self.password)
                        bufsize = 1024
                        local_file = open(local_path + k, 'wb')
                        ftp.retrbinary('RETR %s' % (
                            remote_path + k), local_file.write, bufsize)
                        ftp.set_debuglevel(0)
                        local_file.close()
                        LOG.info("downloading %s !!!" % (remote_path + k))
                except Exception:
                    bufsize = 1024
                    ftp = FTP()
                    ftp.connect(self.ip, self.port)
                    ftp.login(self.user, self.password)
                    local_file = open(local_path + k, 'wb')
                    ftp.retrbinary('RETR %s' % (remote_path + k),
                                   local_file.write, bufsize)
                    ftp.set_debuglevel(0)
                    local_file.close()
                    LOG.info("downloading %s !!!" % (remote_path + k))
            else:
                LOG.info("%s %s check successfully!!!" %
                         (current_time, remote_path))

然后使用代码在这里

from tel.api.ftp_utils import ftp as FTP

# Download zip_file from FTP
ip = CONF.upf.ip
port = CONF.upf.port
user = CONF.upf.user
password = CONF.upf.password
ftp = FTP(ip, port, user, password)
remote_path = "raypick/5GC_GD_HUAWEI.zip"
local_path = CONF.upf.local_path
tmp_file_path = local_path + remote_path.split('/')[-1]
ftp.ftp_download(remote_path, local_path)

关于 ftp,部分功能解释如下

ftp登陆连接
from ftplib import FTP            #加载ftp模块
ftp=FTP()                         #设置变量
ftp.set_debuglevel(2)             #打开调试级别2,显示详细信息
ftp.connect("IP","port")          #连接的ftp sever和端口
ftp.login("user","password")      #连接的用户名,密码
print ftp.getwelcome()            #打印出欢迎信息
ftp.cmd("xxx/xxx")                #进入远程目录
bufsize=1024                      #设置的缓冲区大小
filename="filename.txt"           #需要下载的文件
file_handle=open(filename,"wb").write #以写模式在本地打开文件
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0)             #关闭调试模式
ftp.quit()                        #退出ftp
 
ftp相关命令操作
ftp.cwd(pathname)                 #设置FTP当前操作的路径
ftp.dir()                         #显示目录下所有目录信息
ftp.nlst()                        #获取目录下的文件
ftp.mkd(pathname)                 #新建远程目录
ftp.pwd()                         #返回当前所在位置
ftp.rmd(dirname)                  #删除远程目录
ftp.delete(filename)              #删除远程文件
ftp.rename(fromname, toname)#将fromname修改名称为toname。
ftp.storbinaly("STOR filename.txt",file_handel,bufsize)  #上传目标文件
ftp.retrbinary("RETR filename.txt",file_handel,bufsize)  #下载FTP文件

原文链接:https://blog.csdn.net/weixin_44388689/article/details/120769482







所属网站分类: 技术文章 > 博客

作者:676yyj

链接:https://www.pythonheidong.com/blog/article/1060689/088e359d80d95ec22379/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)