当前位置:网站首页>ftp、sftp文件传输
ftp、sftp文件传输
2022-07-04 17:43:00 【我不恨凡尘】
ftp
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from ftplib import FTP
import os
class MyFtp:
def __init__(self, host=None, port=21, username=None, password=None):
self.ftp = FTP()
# 重新设置下编码方式
self.ftp.encoding = 'utf-8'
try:
# 0主动模式 1 #被动模式
self.ftp.set_pasv(True)
self.ftp.connect(host, port)
self.ftp.login(username, password)
print('成功连接到:', host, "登录成功...")
except Exception as err:
print("FTP 连接或登录失败,错误描述为:", err)
def __del__(self):
print("关闭连接")
self.ftp.quit()
def is_same_size(self, local_file, remote_file):
# 判断文件大小是否相等
try:
remote_file_size = self.ftp.size(remote_file)
except Exception as err:
# print("is_same_size() 错误描述为:%s" % err)
remote_file_size = -1
try:
local_file_size = os.path.getsize(local_file)
except Exception as err:
# print("is_same_size() 错误描述为:%s" % err)
local_file_size = -1
# print(f'local_file_size:{local_file_size}, remote_file_size:{remote_file_size}')
if remote_file_size == local_file_size:
return 1
else:
return 0
def download_file(self, remote_file, local_file):
remote_file = remote_file if self.ftp.pwd() == "/" else self.ftp.pwd() + "/" + remote_file
print(f"下载文件: {remote_file} ---> {local_file}")
if self.is_same_size(local_file, remote_file):
print("文件大小相同,此文件跳过")
else:
try:
buf_size = 1024
file_handler = open(local_file, 'wb')
self.ftp.retrbinary(f'RETR {remote_file}', file_handler.write, blocksize=buf_size)
file_handler.close()
except Exception as err:
print(f'下载文件出错,出现异常:{err}')
def download_dir(self, remote_dir, local_dir):
remote_dir = remote_dir if self.ftp.pwd() == "/" else self.ftp.pwd() + "/" + remote_dir
print(f"下载目录: {remote_dir} ---> {local_dir}")
try:
self.ftp.cwd(remote_dir)
print(f'切换至目录: {self.ftp.pwd()}')
except Exception as err:
print(f'远程目录{remote_dir}不存在,继续...,具体错误描述为:{err}')
return
os.makedirs(local_dir, exist_ok=True)
remote_dirs = []
self.ftp.dir(".", remote_dirs.append)
remote_list = list(map(lambda x: x.split()[-1], remote_dirs))
print(f'远程目录列表: {remote_list}')
for remote_detail in remote_dirs:
file_type = remote_detail[0]
file_name = remote_detail.split()[-1]
local_path = os.path.join(local_dir, file_name)
if file_type == "d":
self.download_dir(file_name, local_path)
elif file_type == '-':
self.download_file(file_name, local_path)
self.ftp.cwd("..")
print("返回上层目录: ", self.ftp.pwd())
def upload_file(self, local_file, remote_file):
# 上传文件
remote_file = remote_file if self.ftp.pwd() == "/" else self.ftp.pwd() + "/" + remote_file
print(f"上传文件: {local_file} ---> {remote_file}")
if not os.path.isfile(local_file):
print(f'{local_file} 不存在')
return
if self.is_same_size(local_file, remote_file):
print('文件大小相同,此文件跳过')
return
try:
buf_size = 1024
file_handler = open(local_file, 'rb')
self.ftp.storbinary(f'STOR {remote_file}', file_handler, buf_size)
file_handler.close()
except Exception as err:
print(f'上传文件出错,出现异常:{err}')
def upload_dir(self, local_dir, remote_dir):
# 上传目录
remote_dir = remote_dir if self.ftp.pwd() == "/" else self.ftp.pwd() + "/" + remote_dir
print(f"上传目录: {local_dir} ---> {remote_dir}")
if not os.path.isdir(local_dir):
print(f'本地目录 {local_dir} 不存在')
return
try:
self.ftp.mkd(remote_dir)
except Exception as err:
print(f"创建远程目录{remote_dir}失败,错误描述为:{err}")
return
self.ftp.cwd(remote_dir)
print('切换至远程目录: ', self.ftp.pwd())
local_name_list = os.listdir(local_dir)
print("本地目录列表: ", local_name_list)
for local_name in local_name_list:
local_name_path = os.path.join(local_dir, local_name)
if os.path.isdir(local_name_path):
self.upload_dir(local_name_path, local_name)
else:
self.upload_file(local_name_path, local_name)
self.ftp.cwd("..")
print("返回上层目录: ", self.ftp.pwd())sftp
#!/usr/bin/python
# coding=utf-8
import paramiko
import os
import stat
class MySftp:
def __init__(self, host=None, port=22, username=None, password=None):
self.sftp_connect = paramiko.Transport((host, port))
self.sftp_connect.connect(username=username, password=password)
self.sftp = paramiko.SFTPClient.from_transport(self.sftp_connect)
print('成功连接到:', host, "登录成功...")
def __del__(self):
print("关闭连接")
self.sftp_connect.close()
def is_same_size(self, local_file, remote_file):
# 判断文件大小是否相等
try:
remote_file_size = self.sftp.stat(remote_file).st_size
except Exception as err:
# print("is_same_size() 错误描述为:%s" % err)
remote_file_size = -1
try:
local_file_size = os.path.getsize(local_file)
except Exception as err:
# print("is_same_size() 错误描述为:%s" % err)
local_file_size = -1
# print(f'local_file_size:{local_file_size}, remote_file_size:{remote_file_size}')
if remote_file_size == local_file_size:
return 1
else:
return 0
def download_file(self, remote_file, local_file):
remote_file = self.sftp.getcwd() + "/" + remote_file if self.sftp.getcwd() else remote_file
print(f"下载文件:{remote_file} ---> {local_file}")
if self.is_same_size(local_file, remote_file):
print("文件已存在且文件已存在且文件大小相同,此文件跳过")
return
try:
self.sftp.get(remote_file, local_file)
except Exception as err:
print(f'下载文件出错,出现异常:{err}')
def download_dir(self, remote_dir, local_dir):
remote_dir = self.sftp.getcwd() + "/" + remote_dir if self.sftp.getcwd() else remote_dir
print(f"下载目录: {remote_dir} ---> {local_dir}")
try:
self.sftp.chdir(remote_dir)
print(f'切换至目录: {self.sftp.getcwd()}')
except Exception as err:
print(f'远程目录{remote_dir}不存在,继续...,具体错误描述为:{err}')
return
os.makedirs(local_dir, exist_ok=True)
remote_dirs = self.sftp.listdir(remote_dir)
print(f'远程目录列表: {remote_dirs}')
for remote_path in remote_dirs:
local_path = os.path.join(local_dir, remote_path)
if str(self.sftp.stat(remote_path))[0] == "d":
self.download_dir(remote_path, local_path)
elif str(self.sftp.stat(remote_path))[0] == "-":
self.download_file(remote_path, local_path)
self.sftp.chdir("..")
print("返回上层目录: ", self.sftp.getcwd())
def upload_file(self, local_file, remote_file):
remote_file = self.sftp.getcwd() + "/" + remote_file if self.sftp.getcwd() else remote_file
print(f"上传文件: {local_file} ---> {remote_file}")
if not os.path.isfile(local_file):
print(f"本地文件 {local_file} 不存在")
return
if self.is_same_size(local_file, remote_file):
print('文件已存在且文件大小相同,此文件跳过')
return
try:
self.sftp.put(local_file, remote_file)
except Exception as err:
print(f'上传文件出错,出现异常:{err}')
def upload_dir(self, local_dir, remote_dir):
remote_dir = self.sftp.getcwd() + "/" + remote_dir if self.sftp.getcwd() else remote_dir
print(f"上传目录: {local_dir} ---> {remote_dir}")
if not os.path.isdir(local_dir):
print(f'本地目录 {local_dir} 不存在')
return
if not self.is_sftp_dir(remote_dir):
self.sftp.mkdir(remote_dir)
self.sftp.chdir(remote_dir)
print('切换至远程目录: ', self.sftp.getcwd())
local_name_list = os.listdir(local_dir)
print("本地目录列表: ", local_name_list)
for local_name in local_name_list:
local_name_path = os.path.join(local_dir, local_name)
if os.path.isdir(local_name_path):
self.upload_dir(local_name_path, local_name)
else:
self.upload_file(local_name_path, local_name)
self.sftp.chdir("..")
print("返回上层目录: ", self.sftp.getcwd())
def is_sftp_dir(self, remote_dir):
try:
if stat.S_ISDIR(self.sftp.stat(remote_dir).st_mode): # 如果remote_dir存在且为目录,则返回True
return True
except:
return False边栏推荐
- 自由小兵儿
- Pb extended DLL development (super chapter) (VII)
- In flinksql, in addition to data statistics, is the saved data itself a state
- Unity editor extends C to traverse all pictures in folders and subdirectories
- Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
- 从实时应用角度谈通信总线仲裁机制和网络流控
- 2022养生展,健康展,北京大健康展,健康产业展11月举办
- 数组中的第K个最大元素
- Lex and yacc based lexical analyzer + parser
- ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
猜你喜欢

Scala基础教程--13--函数进阶

Li Kou brush question diary /day2/2022.6.24

2022CoCa: Contrastive Captioners are Image-Text Fountion Models

Scala basic tutorial -- 15 -- recursion

神经网络物联网应用技术就业前景【欢迎补充】

Angry bird design based on unity

My colleagues quietly told me that flying Book notification can still play like this

Rookie post station management system based on C language

Scala基础教程--15--递归

ThreadLocal原理与使用
随机推荐
2022健康展,北京健博会,中国健康展,大健康展11月13日
1672. Total assets of the richest customers
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
Caché WebSocket
基于unity的愤怒的小鸟设计
模板_大整数减法_无论大小关系
使用canal配合rocketmq监听mysql的binlog日志
In flinksql, in addition to data statistics, is the saved data itself a state
MXNet对GoogLeNet的实现(并行连结网络)
Scala基础教程--15--递归
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
redis分布式锁的8大坑总结梳理
Li Kou brush question diary /day2/2022.6.24
基于C语言的菜鸟驿站管理系统
2022 ByteDance daily practice experience (Tiktok)
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
从实时应用角度谈通信总线仲裁机制和网络流控
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
Go微服务(二)——Protobuf详细入门