python 利用SFTP 读取 、备份、删除txt文件


# -*- coding: utf-8 -*-
import tools
import paramiko
from cStringIO import StringIO
import logging

_logger = logging.getLogger(__name__)


def get_conf_parser():
    """
    读取SFTP配置参数
    :return:
    """
    config = tools.config

    host = config.get("host", None)
    port = config.get("port", None)
    user = config.get("user", None)
    pwd = config.get("pwd", None)
    download_path = config.get("download_path", None)
    backup_path = config.get("backup_path", None)
    return host, port, user, pwd, download_path, backup_path


def connect_sftp(host, port, username, password):
    """
    连接SFTP
    :param host:
    :param port:
    :param username:
    :param password:
    :return:
    """
    try:
        sf = paramiko.Transport((host, int(port)))
        sf.connect(username=username, password=password)
        sftp = paramiko.SFTPClient.from_transport(sf)
        _logger.info("成功连接SFTP:%s", host)
        return sftp
    except Exception as e:
        _logger.info(e)
        return False


def put_sftp_file(sftp, f1, backup_path):
    """
    上传数据到备份文件
    :param f1:数据对象
    :param backup_path:
    :return:
    """
    try:
        sftp.putfo(f1, backup_path)
        _logger.info("成功备份SFTP文件:%s", backup_path)
        return True
    except Exception as e:
        _logger.info(e)
        return False


def remove_sftp_file(sftp, download_path):
    """
    删除已被读取的数据文件
    :param backup_path:
    :return:
    """
    try:
        sftp.remove(download_path)
        _logger.info("成功删除SFTP文件:%s", download_path)
        return True
    except Exception as e:
        _logger.info(e)
        return False


def get_filename(sftp, download_path):
    """
    读取文件目录下所有文件名
    :return:
    """
    try:
        return sftp.listdir(download_path)
    except Exception as e:
        _logger.info(e)
        return False


def read_sftp_file(sftp, download_file, backup_file):
    f = []
    try:
        f1 = StringIO()
        sftp.getfo(download_file, f1)
        f1.seek(0)
        put_sftp_file(sftp, f1, backup_file)
        f1.seek(0)
        _logger.info("成功读取SFTP文件: %s", download_file)
        while True:
            line = f1.readline()
            if line:
                f.append(line)
            else:
                break
    except Exception, e:
        _logger.info(e)
    return f

你可能感兴趣的:(python)