import plac import pysftp import json from pathlib import Path from dataclasses import dataclass @dataclass class FtpConfig: server: str username: str password: str path: str class ftp: commands = ['upload', 'download_dir'] def __init__(self): self.base_dir = Path(__file__).parent # cfg = json.load(open(self.base_dir.joinpath('ftp_config.json'), 'r')) # self.ftp_cfg = FtpConfig(**cfg) self.ftp_cfg = FtpConfig("ftp.global-cube.com", "p33781016-vm", "Gcbs12ma-vm2020!!", "/") @plac.pos('filename', '', type=str) def upload(self, filename='CARLO.csv'): file_path = Path(filename) with pysftp.Connection(self.ftp_cfg.server, self.ftp_cfg.username, self.ftp_cfg.password) as ftp_conn, open(file_path, 'rb') as f: ftp_conn.cwd(self.ftp_cfg.path) res = ftp_conn.storbinary(f'STOR {file_path.name}', f) print(res) @plac.pos('path_from', '', type=str) @plac.pos('path_to', '', type=str) def download_dir(self, path_from='/server2019', path_to='./'): cnopts = pysftp.CnOpts() cnopts.hostkeys = None with pysftp.Connection(self.ftp_cfg.server, username=self.ftp_cfg.username, password=self.ftp_cfg.password, cnopts=cnopts) as ftp_conn: ftp_conn.cwd(path_from) files_list = ftp_conn.listdir() print(files_list) ftp_conn.get_d(path_from, path_to) if __name__ == '__main__': # plac.Interpreter.call(ftp) ftp().download_dir()