print("Python教程:http://c.biancheng.net/python/")
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from ftplib import FTP
import os
import sys
import time
import socket
class MyFTP:
def __init__(self, host, port=21):
""" 初始化 FTP 客户端
参数:
host:ip地址
port:端口号
"""
# print("__init__()---> host = %s ,port = %s" % (host, port))
self.host = host
self.port = port
self.ftp = FTP()
# 重新设置下编码方式
self.ftp.encoding = 'gbk'
self.log_file = open("d:/ftp/log.txt", "a")
self.file_list = []
def login(self, username, password):
""" 初始化 FTP 客户端
参数:
username: 用户名
password: 密码
"""
try:
timeout = 60
socket.setdefaulttimeout(timeout)
# 0主动模式 1 #被动模式
self.ftp.set_pasv(1)
# 打开调试级别2,显示详细信息
self.ftp.set_debuglevel(2)
self.debug_print('开始尝试连接到 %s : %s' % (self.host , self.port))
self.ftp.connect(self.host, self.port)
self.debug_print('成功连接到 %s' % self.host)
self.debug_print('开始尝试登录到 %s' % self.host)
self.ftp.login(username, password)
self.debug_print('成功登录到 %s' % self.host)
self.debug_print(self.ftp.welcome)
except Exception as err:
self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
pass
def is_same_size(self, local_file, remote_file):
"""判断远程文件和本地文件大小是否一致
参数:
local_file: 本地文件
remote_file: 远程文件
"""
try:
remote_file_size = self.ftp.size(remote_file)
except Exception as err:
# self.debug_print("is_same_size() 错误描述为:%s" % err)
remote_file_size = -1
try:
local_file_size = os.path.getsize(local_file)
except Exception as err:
# self.debug_print("is_same_size() 错误描述为:%s" % err)
local_file_size = -1
self.debug_print('local_file_size:%d , remote_file_size:%d' % (local_file_size, remote_file_size))
if remote_file_size == local_file_size:
return 1
else:
return 0
def download_file(self, local_file, remote_file):
"""从ftp下载文件
参数:
local_file: 本地文件
remote_file: 远程文件
"""
self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
if self.is_same_size(local_file, remote_file):
self.debug_print('%s 文件大小相同,无需下载' % local_file)
return
else:
try:
self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
buf_size = 1024
file_handler = open(local_file, 'wb')
self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
file_handler.close()
except Exception as err:
self.debug_print('下载文件出错,出现异常:%s ' % err)
return
def download_file_tree(self, local_path, remote_path):
"""从远程目录下载多个文件到本地目录
参数:
local_path: 本地路径
remote_path: 远程路径
"""
print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
try:
self.ftp.cwd(remote_path)
except Exception as err:
self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
return
if not os.path.isdir(local_path):
self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
os.makedirs(local_path)
self.debug_print('切换至目录: %s' % self.ftp.pwd())
self.file_list = []
# 方法回调
self.ftp.dir(self.get_file_list)
remote_names = self.file_list
self.debug_print('远程目录 列表: %s' % remote_names)
for item in remote_names:
file_type = item[0]
file_name = item[1]
local = os.path.join(local_path, file_name)
if file_type == 'd':
print("download_file_tree()---> 下载目录: %s" % file_name)
self.download_file_tree(local, file_name)
elif file_type == '-':
print("download_file()---> 下载文件: %s" % file_name)
self.download_file(local, file_name)
self.ftp.cwd("..")
self.debug_print('返回上层目录 %s' % self.ftp.pwd())
return True
def upload_file(self, local_file, remote_file):
"""从本地上传文件到ftp
参数:
local_path: 本地文件
remote_path: 远程文件
"""
if not os.path.isfile(local_file):
self.debug_print('%s 不存在' % local_file)
return
#if self.is_same_size(local_file, remote_file):
# self.debug_print('跳过相等的文件: %s' % local_file)
# return
buf_size = 1024
file_handler = open(local_file, 'rb')
self.ftp.storbinary('STOR %s' % remote_file, file_handler, buf_size)
file_handler.close()
self.debug_print('上传: %s' % local_file + "成功!")
def upload_file_tree(self, local_path, remote_path):
"""从本地上传目录下多个文件到ftp
参数:
local_path: 本地路径
remote_path: 远程路径
"""
if not os.path.isdir(local_path):
self.debug_print('本地目录 %s 不存在' % local_path)
return
"""
创建服务器目录
"""
try:
self.ftp.cwd(remote_path) # 切换工作路径
except Exception as e:
base_dir, part_path = self.ftp.pwd(), remote_path.split('/')
for p in part_path[1:-1]:
base_dir = base_dir + p + '/' # 拼接子目录
try:
self.ftp.cwd(base_dir) # 切换到子目录, 不存在则异常
except Exception as e:
print('INFO:', e)
self.ftp.mkd(base_dir) # 不存在创建当前子目录
#self.ftp.cwd(remote_path)
self.debug_print('切换至远程目录: %s' % self.ftp.pwd())
local_name_list = os.listdir(local_path)
self.debug_print('本地目录list: %s' % local_name_list)
#self.debug_print('判断是否有服务器目录: %s' % os.path.isdir())
for local_name in local_name_list:
src = os.path.join(local_path, local_name)
print("src路径=========="+src)
if os.path.isdir(src):
try:
self.ftp.mkd(local_name)
except Exception as err:
self.debug_print("目录已存在 %s ,具体错误描述为:%s" % (local_name, err))
self.debug_print("upload_file_tree()---> 上传目录: %s" % local_name)
self.debug_print("upload_file_tree()---> 上传src目录: %s" % src)
self.upload_file_tree(src, local_name)
else:
self.debug_print("upload_file_tree()--->
没有合适的资源?快使用搜索试试~ 我知道了~
要通过FTP上传文件,可以按照以下描述进行操作: 导入所需模块:首先,导入ftplib模块来实现FTP连接和操作。 建立FTP连接:使用ftplib模块中的FTP类创建一个FTP对象,并使用它来建立与FTP服务器的连接。需要提供FTP服务器的地址、用户名和密码等登录信息。 登录FTP服务器:使用FTP对象的login方法来登录到FTP服务器。根据FTP服务器的设置,可能需要提供用户名和密码进行身份验证。 切换到目标目录:使用FTP对象的cwd方法切换到要上传文件的目标目录。如果需要上传到指定目录下的子目录,可以使用适当的路径进行切换。 打开本地文件:使用内置的open函数打开要上传的本地文件。可以提供文件的绝对路径或相对路径。 上传文件:使用FTP对象的storbinary方法来将本地文件上传到FTP服务器。该方法接受本地文件的文件对象作为参数。 关闭本地文件:上传完成后,关闭本地文件,释放资源。 关闭FTP连接:使用FTP对象的quit方法来关闭与FTP服务器的连接。 请注意,在实际代码编写中,还需要考虑异常处理、进度条显示、文件重命名等方面的需求。此外,如果需要上
资源推荐
资源详情
资源评论




















收起资源包目录



共 1 条
- 1
资源评论


童小纯
- 粉丝: 3w+
- 资源: 289
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
