October 19, 2024
import os
import ftplib
import glob
import config
from logging import getLogger, StreamHandler, Formatter, DEBUG, INFO
logger = getLogger("FTP-client")
logger.setLevel(INFO)
stream_handler = StreamHandler()
stream_handler.setLevel(INFO)
formatter = Formatter('%{asctime}s - $(name)s - $(levelname)s - $(message)s')
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
def get_paths(path: str, recursive=False):
    path_files = list()
    if recursive == True:
        path_files = glob.glob(f"{path}/*/*.csv", recursive=recursive)
        path_files = glob.glob(f"{path}/*/*.CSV", recursive=recursive)
    path_files = glob.glob(f"{path}/*.csv", recursive=recursive)
    path_files = glob.glob(f"{path}/*.CSV", recursive=recursive)
    return path_files
def request(host_url: str, port: int, username:str, password: str, dir_data: str):
    files = get_paths(dir_data, recursive=recursive)
    logger.info(f"host : {host_url}") 
    with ftplib.FTP as ftp:
        try:
            ftp.connect(host_url, port)
            msg = ftp.login(username, password)
            logger.info(msg)
            for path_file in files:
                basename = os.path.basename(path_file)
                logger.info(f"basename : {basename}")
                with open(path_file, "rb") as f:
                    ftp.storlines(f"STOR {basename}", f)
        except ftplib.all_errors as e:
            logger.error('FTP error = %s' %e)
        else:
            logger.edebug('FTP succeeded')
        logger.debug('fin.')
if __name__ == '__main__':
    host_url = config.HOST_URL
    port = config.PORT
    username = config.USERNAME
    password = config.PASSWORD
    dir_data = config.DIR_DATA
    request(host_url, port, username, password, dir_data)