DEV Community


Posted on • Updated on

Download data from ftp server with Python

Recently, I needed to download the latest version of software from a ftp server then install it into tested machine.

I think there might be someone would need to know this so today I'm gonna share with you how to download data from ftp server with Python.

The following post might be how to install the app automatically.

I.What is ftp server:

You might want to read this but basically ftp server is where you store the files

II.FTPlib in Python:

You might want to go to official doc for ftplib in here

We're gonna use ftplib to interact with ftp server

III.Real world example:
Below is a sample working ftp server

Alt Text

The scenario is this, we need to go to that server, find the latest version of file, and download it.

If the latest version of file is already downloaded, no need to download it again.

1.Store the downloaded version name into yaml file:

Sample configuration file where we have coccoc_dev_version


coccoc_dev_version: 79.0.3945.98
  admin_password: '123456'
  admin_username: admin

Enter fullscreen mode Exit fullscreen mode

Self-made utils for working with yaml files

class YamlUtils:

    def __new__(cls):

        if not hasattr(cls, 'instance'):
            cls.instance = super(YamlUtils, cls).__new__(cls)

        return cls.instance

    def read_data_from_file(self, file):
        with open(file) as stream:
                return yaml.safe_load(stream)
            except yaml.YAMLError as exc:

    def write_data_to_file(self, loaded, file):
        with open(file, 'wb') as stream:
                return yaml.safe_dump(loaded, stream, default_flow_style=False
                                      , explicit_start=True, allow_unicode=True, encoding='utf-8')
            except yaml.YAMLError as exc:

Enter fullscreen mode Exit fullscreen mode

Override latest version of file to yaml:

yaml_made_utils = YamlUtils()
configuration_info = None
configuration_path = '/resources/configurations.yaml'

def get_configurations_info():
    if configuration_info is None:
        return yaml_made_utils.read_data_from_file(os.getcwd() + configuration_path)
        return configuration_info

class CocCocConfigs:
    COCCOC_DEV_VERSION = get_configurations_info()['coccoc_dev_version']

    def update_coccoc_dev_version(coccoc_version):
        data = get_configurations_info()
        data['coccoc_dev_version'] = coccoc_version
        return yaml_made_utils.write_data_to_file(data, os.getcwd() + configuration_path)

Enter fullscreen mode Exit fullscreen mode

2.Download the latest version of file if it's not downloaded yet

def download_latest_coccoc_dev_installer():
    coccoc_installer_name = "standalone_coccoc_en.exe"
    from ftplib import FTP
    with FTP('') as ftp:
        ftp.login('anonymous', '')
        folders_list = ftp.nlst()
        coccoc_download_folder_list = get_list_coccoc_installer_dirs(folders_list)
        latest_coccoc_download_dir = coccoc_download_folder_list[-1]
        from configs.yaml_configs import CocCocConfigs
        if CocCocConfigs.COCCOC_DEV_VERSION not in latest_coccoc_download_dir:
                ftp.retrbinary("RETR " + coccoc_installer_name,
                               open(f"C:\\coccoc-dev\\{coccoc_installer_name}", 'wb').write)
                print(f"Error download coccoc binary for {coccoc_installer_name}")
            return latest_coccoc_download_dir
            return "Already latest"

Enter fullscreen mode Exit fullscreen mode

Notice we need to provide username and password along with the ftp host name like:

with FTP('') as ftp:
        ftp.login('anonymous', '')
Enter fullscreen mode Exit fullscreen mode

3.Main function to download file, and updated version name into the configurations file:

def main():
    coccoc_version = download_latest_coccoc_dev_installer()
    if coccoc_version not in "Already latest":

if __name__ == "__main__":

Enter fullscreen mode Exit fullscreen mode

You can checkout the full source code in github in here

Hope this helps.


Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :

This will help me to contributing more valued contents.

Top comments (0)