python从FTP下载文件

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
FTP常用操作
"""
from ftplib import FTP
import os
class FTP_OP(object):
    def __init__(self, host, username, password, port):
        """
        初始化ftp
    :param host: ftp主机ip
    :param username: ftp用户名
    :param password: ftp密码
    :param port:  ftp端口 (默认21)
    """
    self.host = host
    self.username = username
    self.password = password
    self.port = port
    def ftp_connect(self):
    """
    连接ftp
    :return:
    """
    ftp = FTP()
    ftp.set_debuglevel(0)  # 不开启调试模式
    ftp.connect(host=self.host, port=self.port)  # 连接ftp
    ftp.login(self.username, self.password)  # 登录ftp
    return ftp
    def download_file(self, ftp_file_path, dst_file_path):
    """
    从ftp下载文件到本地
    :param ftp_file_path: ftp下载文件路径
    :param dst_file_path: 本地存放路径
    :return:
    """
    buffer_size = 10240  #默认是8192
    ftp = self.ftp_connect()
    print ftp.getwelcome()  #显示登录ftp信息
    file_list = ftp.nlst(ftp_file_path)
    for file_name in file_list:
        ftp_file = os.path.join(ftp_file_path, file_name)
        write_file = os.path.join(dst_file_path, file_name)
        print file_name
        if file_name.find('.jpg')>-1 and not os.path.exists(write_file):
            print "file_name:"+file_name
            #ftp_file = os.path.join(ftp_file_path, file_name)
            #write_file = os.path.join(dst_file_path, file_name)
            with open(write_file, "wb") as f:
                ftp.retrbinary('RETR {0}'.format(ftp_file), f.write, buffer_size)
                f.close()
    ftp.quit()

if __name__ == '__main__':
    host = "10.201.xx.xx"
    username = "JKT"
    password = "E2TS"
    port = "9999"
    ftp_file_path = "/upload/20160726"
    dst_file_path = "/home/gdmt/mastercom/py/tmp"
    ftp = FTP_OP(host=host, username=username, password=password, port=port)
    ftp.download_file(ftp_file_path=ftp_file_path, dst_file_path=dst_file_path)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值