Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

cuongld2
cuongld2

Posted on • Edited 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 readthis but basically ftp server is where you store the files

II.FTPlib in Python:

You might want to go to official doc for ftplib inhere

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

#configurations.yaml--------coccoc_dev_version:79.0.3945.98jenkins:admin_password:'123456'admin_username:admindomain:http://10.0.12.96:8080
Enter fullscreen modeExit fullscreen mode

Self-made utils for working with yaml files

classYamlUtils:def__new__(cls):ifnothasattr(cls,'instance'):cls.instance=super(YamlUtils,cls).__new__(cls)returncls.instancedefread_data_from_file(self,file):withopen(file)asstream:try:returnyaml.safe_load(stream)exceptyaml.YAMLErrorasexc:print(exc)defwrite_data_to_file(self,loaded,file):withopen(file,'wb')asstream:try:returnyaml.safe_dump(loaded,stream,default_flow_style=False,explicit_start=True,allow_unicode=True,encoding='utf-8')exceptyaml.YAMLErrorasexc:print(exc)
Enter fullscreen modeExit fullscreen mode

Override latest version of file to yaml:

yaml_made_utils=YamlUtils()configuration_info=Noneconfiguration_path='/resources/configurations.yaml'defget_configurations_info():ifconfiguration_infoisNone:returnyaml_made_utils.read_data_from_file(os.getcwd()+configuration_path)else:returnconfiguration_infoclassCocCocConfigs:COCCOC_DEV_VERSION=get_configurations_info()['coccoc_dev_version']@staticmethoddefupdate_coccoc_dev_version(coccoc_version):data=get_configurations_info()data['coccoc_dev_version']=coccoc_versionreturnyaml_made_utils.write_data_to_file(data,os.getcwd()+configuration_path)
Enter fullscreen modeExit fullscreen mode

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

defdownload_latest_coccoc_dev_installer():coccoc_installer_name="standalone_coccoc_en.exe"fromftplibimportFTPwithFTP('browser3v.dev.itim.vn')asftp:ftp.login('anonymous','')ftp.cwd('corom')folders_list=ftp.nlst()coccoc_download_folder_list=get_list_coccoc_installer_dirs(folders_list)latest_coccoc_download_dir=coccoc_download_folder_list[-1]fromconfigs.yaml_configsimportCocCocConfigsifCocCocConfigs.COCCOC_DEV_VERSIONnotinlatest_coccoc_download_dir:ftp.cwd(f"{latest_coccoc_download_dir}/installers")try:ftp.retrbinary("RETR"+coccoc_installer_name,open(f"C:\\coccoc-dev\\{coccoc_installer_name}",'wb').write)except:print(f"Error download coccoc binary for {coccoc_installer_name}")returnlatest_coccoc_download_direlse:return"Already latest"
Enter fullscreen modeExit fullscreen mode

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

withFTP('browser3v.dev.itim.vn')asftp:ftp.login('anonymous','')
Enter fullscreen modeExit fullscreen mode

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

defmain():coccoc_version=download_latest_coccoc_dev_installer()ifcoccoc_versionnotin"Already latest":CocCocConfigs.update_coccoc_dev_version(coccoc_version)else:passif__name__=="__main__":main()
Enter fullscreen modeExit fullscreen mode

You can checkout the full source code in github inhere

Hope this helps.

Peace!!!

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Learning ishard work, but everything you learn is yours and will make subsequent learningeasier.
  • Location
    Hanoi, Vietnam
  • Joined

More fromcuongld2

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp