美文网首页
python如何上传或下载ftp文件

python如何上传或下载ftp文件

作者: 轻丨尘 | 来源:发表于2021-08-09 16:19 被阅读0次

    需要借助Paramiko,Paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令、文件传输等功能

    默认Python没有自带,需要手动安装:
    pip3 install paramiko

    上传文件

    # -*- coding: utf-8 -*-
    import paramiko
    
    def sftp_upload_file(host,user,password,server_path, local_path,timeout=10):
        """
        上传文件,注意:不支持文件夹
        :param host: 主机名
        :param user: 用户名
        :param password: 密码
        :param server_path: 远程路径,比如:/home/sdn/tmp.txt
        :param local_path: 本地路径,比如:D:/text.txt
        :param timeout: 超时时间(默认),必须是int类型
        :return: bool
        """
        try:
            t = paramiko.Transport((host, 22))
            t.banner_timeout = timeout
            t.connect(username=user, password=password)
            sftp = paramiko.SFTPClient.from_transport(t)
            sftp.put(local_path, server_path)
            t.close()
            return True
        except Exception as e:
            print(e)
            return False
    

    下载文件

    def sftp_down_file(host,user,password,server_path, local_path,timeout=10):
        """
        下载文件,注意:不支持文件夹
        :param host: 主机名
        :param user: 用户名
        :param password: 密码
        :param server_path: 远程路径,比如:/home/sdn/tmp.txt
        :param local_path: 本地路径,比如:D:/text.txt
        :param timeout: 超时时间(默认),必须是int类型
        :return: bool
        """
        try:
            t = paramiko.Transport((host,port))
            t.banner_timeout = timeout
            t.connect(username=user,password=password)
            sftp = paramiko.SFTPClient.from_transport(t)
            sftp.get(server_path, local_path)
            t.close()
            return True
        except Exception as e:
            print(e)
            return False
    

    Paramiko不支持下载目录,如果要下载目录下所有文件到本地,需要遍历下载

    def sftp_down_all_files(self, remote_path, local_path):
        try:
            t = paramiko.Transport((self.ip, self.port))
            t.connect(username=self.username, password=self.password)
            sftp = paramiko.SFTPClient.from_transport(t)
            all_files = sftp_get_all_files(self, remote_path)
            for x in all_files:
                local_file = local_path + "\\" + x
                remote_file = remote_path +"/" + x
                print  local_file
                sftp.get(remote_file, local_file)
            t.close()
            return True
        except Exception as e:
            print(e)
            return False
    
    def sftp_get_all_files(self, remote_path):
      try:
            t = paramiko.Transport((self.ip, self.port))
            t.connect(username=self.username, password=self.password)
            sftp = paramiko.SFTPClient.from_transport(t)
            all_files = list()
            files = sftp.listdir_attr(remote_path)
            for x in files:
                all_files.append(x.filename)
            return all_files
        except Exception as e:
            print(e)
            return all_files
    

    相关文章

      网友评论

          本文标题:python如何上传或下载ftp文件

          本文链接:https://www.haomeiwen.com/subject/mnbzvltx.html