1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import plac
- import pysftp
- from pathlib import Path
- from dataclasses import dataclass
- import warnings
- @dataclass
- class FtpConfig:
- server: str
- username: str
- password: str
- path: str
- class FtpClient:
- commands = ["upload", "download_dir"]
- def __init__(self):
- self.base_dir = Path(__file__).parent
-
-
-
- self.ftp_cfg = FtpConfig("ftp.global-cube.com", "u1339416173", "dihob#ratuy5kub%", "/")
- warnings.filterwarnings("ignore")
- @plac.pos("filename", "", type=str)
- def upload(self, filename="CARLO.csv"):
- 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(self.ftp_cfg.path)
- ftp_conn.put(filename)
-
- @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__":
-
- FtpClient().download_dir()
|