#!/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 = "xxx"
password = "xxx"
port = ""
ftp_file_path = "/upload/20160726"
dst_file_path = "/home/gdmt/master/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)

最新文章

  1. 我为NET狂官方面试题
  2. codeforces 360 C
  3. Tomcat相关的笔记
  4. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
  5. web开发(二十一)之自定义拦截器的使用
  6. 【spring 区别】ClassXmlAplicationContext和FileSystemXmlApplicationContext的区别
  7. Android中SearchView修改字体颜色
  8. linux tar打包
  9. UNIX V6内核源码剖析——进程
  10. Android的string-array数据源简单使用
  11. Delphi RichEdit的内容保存为图片
  12. webform--常用的控件
  13. SQL中的左连接与右连接有什么区别,点解返回值会不同?(转)
  14. 《JAVA与模式》之命令模式
  15. pytest五:fixture_autouse=True
  16. 【JMeter】【性能测试】参数化,内存溢出问题
  17. AOP 入门
  18. Mysql版本java问题(com.mysql.cj.jdbc.Driver和com.mysql.jdbc.Driver)
  19. validateRequest 相关的作用
  20. 2017.4.4 TCP/IP三次握手,四次挥手

热门文章

  1. python字符串面试题:找出一个字符串中第一个字母和最后一个字符是第一次重复,中间没有重复且最长的子串
  2. Seaborn入门
  3. javascript parseUrl函数解析url获取网址url参数
  4. sench touch 页面跳转
  5. pygame 笔记-7 生命值/血条处理
  6. springmvc中select可以绑定enum中所有数据的方法
  7. iOS WKWebview 网页开发适配指南【转】
  8. ZABBIX 4.0 LTS 部署
  9. Win10系统的DELL平板如何重装WIN10系统
  10. H5调用本地摄像头[转]