python中ftplib库的使用

2023-09-05  本文已影响0人  站在巨人的肩膀上努力着
  1. 在Django应用中建立FTP连接:你可以使用Python的 ftplib库来在Django应用中建立FTP连接。以下是一个简单的例子:

    from ftplib import FTP
    
    ftp = FTP()
    ftp.connect('ftp.example.com')
    ftp.login(user='username', passwd='password')
    
  2. 上传文件:使用ftplibstorbinary方法可以上传文件。以下是一个简单的例子:

    filename = 'file.zip'
    with open(filename, 'rb') as f:
        ftp.storbinary('STOR ' + filename, f)
    ftp.quit()
    
  3. 在目标服务器上解压缩文件:你可以在目标服务器上运行一个简单的脚本来监听新的zip文件,并在新文件到达时自动解压缩。以下是一个使用Python的 zipfile库来解压缩文件的例子:

    import zipfile
    
    def unzip_file(zip_filepath, dest_path):
        with zipfile.ZipFile(zip_filepath, 'r') as zip_ref:
            zip_ref.extractall(dest_path)
    

    你可以将此脚本设置为cron任务,或者使用其他方法使其在新文件到达时自动运行。
    4.ftp处理方法
    getdirs()功能是获取当前路径或者指定路径下的文件、目录

代码大致思路是使用dir_list列表放dir()列出的信息, 然后再将名字的信息过滤出来返回其列表。
注意:将.和..目录去掉

checkFileDir()功能是检查指定路径是目录还是文件

代码大致思路是使用cwd()并检查异常信息判断是文件还是目录。
注意:尽量将路径返回原来的路径。

get_modify_time()功能是得到指定目录、文件或者当前目录、文件的修改时间

代码大致思路是将当前路径或者传入的路径切分成上一级目录的路径和当前的目录或文件的名称,使用retrlines('MLSD', detail_list.append)将上一级目录的路径下所有的目录或文件所有MLSD信息存储到detail_list中,再对比出当前文件或者目录的MLSD信息,然后将修改日期切分出来,由于文件有size、目录没有size,所以需要在切分时区分。
注意:得到的修改时间在windows下并非是时间戳,上述实现也是在windows下实现的,linux可能会不不同,请自行参考思路。

from ftplib import FTP, error_perm
import os
import re


class MyFTP(FTP):
    encoding = "utf-8"

    def getdirs(self, dirpath=None):
        """
        获取当前路径或者指定路径下的文件、目录
        :param args:
        :return:
        """
        if dirpath != None:
            self.cwd(dirpath)
        dir_list = []
        self.dir('.', dir_list.append)
        dir_name_list = [dir_detail_str.split(' ')[-1] for dir_detail_str in dir_list]
        return [file for file in dir_name_list if file != "." and file !=".."]

    def checkFileDir(self, dirpath):
        """
        检查指定路径是目录还是文件
        :param dirpath: 文件路径或目录路径
        :return:返回字符串“File”为文件,“Dir”问文件夹,“Unknow”为无法识别
        """
        rec = ""
        try:
            rec = self.cwd(dirpath)  # 需要判断的元素
            self.cwd("..")  # 如果能通过路劲打开必为文件夹,在此返回上一级
        except error_perm as fe:
            rec = fe  # 不能通过路劲打开必为文件,抓取其错误信息
        finally:
            if "Not a directory" in str(rec):
                return "File"
            elif "Current directory is" in str(rec):
                return "Dir"
            else:
                return "Unknow"

    def get_modify_time(self, dirpath=None):
        """
        得到指定目录、文件或者当前目录、文件的修改时间
        :param dirname:
        :return:
        """
        if dirpath != None:
            if dirpath[-1] == '/':
                dir_path = os.path.split(dirpath[0: -1])[0]
                current_name = os.path.split(dirpath[0: -1])[1]
            else:
                dir_path = os.path.split(dirpath)[0]
                # .strip()是为了避免出现”/ 2095-8757“这种情况,下面匹配不到
                current_name = os.path.split(dirpath)[1].strip()
            self.cwd(dir_path)
        else:
            dirpath = self.pwd()
            dir_path = os.path.split(dirpath)[0]
            current_name = os.path.split(dirpath)[1]
            self.cwd(dir_path)

        detail_list = []
        self.retrlines('MLSD', detail_list.append)

        current_info = ''
        for detail_info in detail_list:
            # 文件和目录不一样
            # 文件从字符串获取名称
            if detail_info.split(';')[3].strip() == current_name:
                current_info = detail_info
                break
        if not current_info:
            for detail_info in detail_list:
                # 目录从字符串获取名称
                if detail_info.split(';')[2].strip() == current_name:
                    current_info = detail_info
        modify_time = re.search(r'modify=(.*);', current_info).group(1)

        return modify_time

5.ftp链接逻辑

from myftp import MyFTP
import os

def connect_ftp(host, port, user, passwd):
    """

    :param host:
    :param port:
    :param user:
    :param passwd:
    :return:
    """
    ftpServer = MyFTP()
    ftpServer.encoding = "utf-8"
    ftpServer.connect(host=host, port=port)
    ftpServer.login(user=user, passwd=passwd)
    return ftpServer

# 连接服务器(参数是你的ftp服务器东西)
ftpServer = connect_ftp(host, port, username, password)

file_modify_t = ftpServer.get_modify_time(dirpath=file_path)
file_date_str = file_modify_t[0:4] + '-' + file_modify_t[4:6] + '-' + file_modify_t[6:8] + ' ' + '00:00:00'
上一篇 下一篇

猜你喜欢

热点阅读