Нужно что бы файлы с SFTP попадали не в один каталог , а создавались точно такие же каталоги как на SFTP на локальном компьютере.
На данном этапе получаем код:
#Host SFTP сервера
host = "test.rebex.net"
#Port SFTP сервера
port = 22
#Имя пользователя для подключения к SFTP серверу
username = "demo"
#Пароль для подключения к SFTP серверу
password = "password"
#######################################################################################################################
import paramiko, os
from stat import S_ISDIR
#Включаем логи
paramiko.util.log_to_file('D:\\Lessons_Python\\paramiko.log')
#Используем функцию для соединения по SSH
ssh = paramiko.SSHClient()
#Получаем ключ для подключения по SSH
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Устанавливаем соединение с sftp
#Используя host ,username, password
sftp_connect = ssh.connect(host, username=username,password=password)
#Открываем соединение с SFTP
sftp=ssh.open_sftp()
def sftp_connect():
#Включаем логи
paramiko.util.log_to_file('D:\\Рaramiko\\paramiko.log')
#Используем функцию для соединения по SSH
ssh = paramiko.SSHClient()
#Получаем ключ для подключения по SSH
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Устанавливаем соединение с sftp
#Используя host ,username, password
sftp_connect = ssh.connect(host, username=username,password=password)
#Открываем соединение с SFTP
sftp=ssh.open_sftp()
def sftp_walk(remotepath):
path=remotepath
files=[]
folders=[]
for f in sftp.listdir_attr(remotepath):
if S_ISDIR(f.st_mode):
folders.append(f.filename)
else:
files.append(f.filename)
if files:
yield path, files
for folder in folders:
new_path=os.path.join(remotepath,folder)
for x in sftp_walk(new_path):
yield x
for path,files in sftp_walk("."):
for file in files:
#sftp.get(remote, local) line for dowloading.
sftp.get(os.path.join(path,file), os.path.join('D:\\Рaramiko\\', file))
Результат 1.Файлы из всех папок на сервера SFTP загружаются в один каталог.
2. Получаем ошибку после выполнения скрипта
C:\Users\Users\AppData\Local\Programs\Python\Python37-32\python.exe D:/Рaramiko/Рaramiko_SFTP.py
C:\Users\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\kex_ecdh_nist.py:39: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
m.add_string(self.Q_C.public_numbers().encode_point())
C:\Users\Pavel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\kex_ecdh_nist.py:96: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.curve, Q_S_bytes
C:\Users\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\kex_ecdh_nist.py:111: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
hm.add_string(self.Q_C.public_numbers().encode_point())