python----FTP文件拉取(new)

在使用FTP模块时候,首先需要定义FTP的实例,才可以对FTP进行操作

from ftplib import FTP
import timeimport os
sys_time = time.time()
sys_time_array = time.localtime(sys_time)
current_time = time.strftime("%Y-%m-%d %H:%M:%S:",sys_time_array)

#FTP登录
def ftp_login(host,port,user,password):                          
    ftp = FTP()                                          #定义一个空的FTP实例
    try:
        ftp.connect(host,port)                           #指定ip、端口,连接FTP
    except:
        print "FTP connect failed!!!"
    try:
        ftp.login(user,password)                         #指定账号、密码,登录FTP           
    except:
        print "FTP login failed!!!"                      
    else:
        return ftp                                       #返回ftp实例

拥有一个FTP实例之后,我们就可以对其进行操作。

#FTP目录全量下载
def ftp_download(ftp,remote_path,local_path):
    try:
        file_list = ftp.nlst(remote_path)                #列出ftp指定目录下的所有文件,这里返回一个列表。
    except:
        print "remote_path error!!!"
    else:
        key = os.path.exists(local_path)                 #检测本地存放文件的目录是否存在,若不存在,则创建。
        if key == 'True':
            pass
        else:
            cmd = 'mkdir ' + local_path + ' -p'
            os.system(cmd)
        print "downloading!!!"
        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上的文件写到本地文件里面
                ftp.set_debuglevel(0)
                local_file.close()                                         #记得要关闭文件
        except:
            print "%s %s download failed!!!" %(current_time,remote_path)
        else:
            print "%s %s download succeed!!!" %(current_time,remote_path)

下面是笔者在工作中编写的一段代码,其功能是简单地实现FTP的下载,上传,文件校验

#!/usr/bin/env python
#-*- coding: utf-8 -*-
from ftplib import FTP
import sys,time,os,hashlib

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

#定义FTP实例
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:
            print "FTP connect failed!!!"
        try:
            ftp.login(self.user,self.password)
        except:
            print "FTP login failed!!!"
        else:
            return ftp

    #FTP下载
    def ftp_download(self,remote_path,local_path):
        ftp = FTP()
        try:
            ftp.connect(self.ip,self.port)
        except:
            print "FTP connect failed!!!"
        try:
            ftp.login(self.user,self.password)
        except:
            print "FTP login failed!!!"
        else:
            try:
                file_list = ftp.nlst(remote_path)
            except:
                print "remote_path error!!!"
            else:
                key = os.path.exists(local_path)
                if str(key) == 'True':
                    pass
                else:
                    os.makedirs(local_path)
                print "downloading!!!"
                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:
                    print "%s %s download failed!!!" %(current_time,remote_path)
                else:
                    print "%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:
            print "FTP connect failed!!!"
        try:
            ftp.login(self.user,self.password)
        except:
            print "FTP login failed!!!"
        else:
            try:
                ftp.mkd(remote_path)
            except:
                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:
                print "%s %s upload failed!!!" %(current_time,local_path)
            else:
                print "%s %s upload successfully!!!" %(current_time,local_path)

    #FTP文件校验
    def check(self,remote_path,local_path):
    #将FTP临时文件下载到本地临时目录
        tmp_path = '/tmp' + remote_path
        ftp = FTP()
        try:
            ftp.connect(self.ip,self.port)
        except:
            print "FTP connect failed!!!"
        try:
            ftp.login(self.user,self.password)
        except:
            print "FTP login failed!!!"
        else:
            try:
                file_list = ftp.nlst(remote_path)
            except:
                print "remote_path error!!!"
            else:
                key = os.path.exists(tmp_path)
                if str(key) == 'True':
                    pass
                else:
                    os.makedirs(tmp_path)
                print "checking!!!"
                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:
                    print "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()
                        print "downloading %s !!!" %(remote_path+k)
                except:
                    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()
                    print "downloading %s !!!" %(remote_path+k)
            else:
                print "%s %s check successfully!!!" %(current_time,remote_path)

客户配置界面

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#-author qicongliang-
import easyftp,time
sys_time = time.time()
sys_time_array = time.localtime(sys_time)
year = sys_time_array.tm_year
mon = sys_time_array.tm_mon
day = sys_time_array.tm_mday

#***
##############################################example#################################################
# 输入FTP相关信息,并得出一个FTP实例,名字叫“FTP1”                                                 #
# - ftp1 = easyftp.ftp('10.172.12.32','21','dqt','dqt')                                              #
#                                                                                                    #
# 调用FTP1的下载功能,并指定FTP目录与本地目录。                                                      #
# - ftp1.ftp_download('DSShare/release/rh2m/999/2018/11/27/','/mnt/data/grid_test/2018/11/27/rh2m/') #
#                                                                                                    #
# 调用FTP1的校验功能,并指定需要校验得FTP目录与对应的本地目录。                                      #
# - ftp1.check('/DSShare/release/rh2m/999/2018/11/27/','/mnt/data/grid_test/2018/11/27/rh2m/')       #
######################################################################################################

#***
#假如您的目录是根据时间日期生产的,请用如下格式编辑您的目录路径
#################################################################################################################################
# 原本的路径。                                                                                                                  #
# - ftp1.check('/DSShare/release/rh2m/999/2018/11/27/','/mnt/data/grid_test/2018/11/27/rh2m/')                                  # 
# 变形后的路径                                                                                                                  #
# - ftp1.ftp_download('DSShare/release/rh2m/999/%s/%s/%s/' %(year,mon,day),'/mnt/data/grid_test/%s/%s/%s/rh2m/' %(year,mon,day))#
#################################################################################################################################


#################################请在下面修改您的相关信息#########################################
#请输入你的FTP信息(IP,端口,用户,密码)
ftp1 = easyftp.ftp('172.16.1.199','21','test','tqw961110')

#请输入需要下载的目录(FTP目录,本地目录)
#ftp1.ftp_download('/test/2018/11/27/','/usr/local/ftp_file/test/2018/11/27/')

#请输入需要上传的目录(FTP目录,本地目录)
ftp1.ftp_upload('/test2/','/usr/local/ftp_file/test2/')

#请输入需要校验的目录(FTP目录,本地目录)
#ftp1.check('/test/2018/11/27/','/usr/local/ftp_file/test/2018/11/27/')

下面附上python在FTP中的一些操作

from ftplib import FTP        # 加载ftp模块
ftp = FTP()                   # 获取FTP对象
ftp.set_debuglevel(2)         # 打开调试级别2,显示详细信息
ftp.connect('IP', PORT)       # 连接ftp,server和端口
ftp.login('user', 'password') # 登录用户
print(ftp.getwelcome())       # 打印欢迎信息
ftp.cmd('xxx/xxx')            # 进入远程目录
bufsize = 1024                # 设置缓存区大小
filename='filename.txt'       # 需要下载的文件
file_handle=open(filename, 'wb').write # 以写的模式在本地打开文件
file.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.rmd(dirname)        # 删除远程目录
ftp.pwd()               # 返回当前所在位置
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://www.cnblogs.com/QicongLiang/p/9999371.html