Python implementation of FTP file timing automatic download

Time:2020-1-22

I have always been inspired by the technical problems I encountered before. Thank you for your selfless sharing. However, I seldom publish, which is limited, but also limits the accumulation and summary of knowledge. In the future, I would like to share more summaries and give back to my blog, and I hope you can criticize me more.

 

1、 Requirements:

Between 15:00 and 17:00 every day, data of a data company is available for download on the day of its FTP release. We need to download the data of that day to the designated local directory in time.

 

2、 Analysis:

1. FTP login, query and download functions shall be realized;

Answer: use the FTP class in the built-in ftplib module;

 

2. It needs to judge whether the file is downloaded;

Answer: use the path.exists method in the OS module;

 

3. It is necessary to judge whether the download task can be performed within the specified time period;

Answer: use the built-in time module to grab the current time and compare it with the specified time;

 

4. The problem of date switching should be considered;

Answer: use the built-in time module to grab the current date and compare it with the date in the variable.

 

3、 Code implementation

#!/usr/bin/env python
 # _*_ coding:utf-8 _*_
 
 '''
 @Time    : 2019-11-11 13:30
 @Author  : Peanut_C
 @FileName: ftp_auto_download.py
 '''
 
 
 import time
 from ftplib import FTP
 import os
 
 
 Remote path = "/ xxx / YY / Z /" remote directory
 Begin time = 1500 task start time
 End time = 1700 task end time
 
 
 Today = time.strftime (""% Y% m% d "") × date of the day
 Today ﹐ file = today + 'test. TXT' ﹐ get the target file name of today's date
 Remote file = Remote path + today file
 Local file = '\ \ \ \ local \' + today + '\ \' + today file
 log_file = 'C:\\log\ftp_log.txt'
 
 
 def ftp_connect():
     "" "for FTP connection" ""
     FTP? Server ='w.x.y.z '? IP address corresponding to FTP site
     Username ='ftpuser 'ා user name
     Password ='ftppass' ා password
     ftp = FTP()
     Ftp.set ﹐ debuglevel (0) ﹐ higher level for easy troubleshooting
     ftp.connect(ftp_server, 21)
     ftp.login(username, password)
     return ftp
 
 def remote_file_exists():
     "" "for FTP site target file presence detection" ""
     ftp = ftp_connect()
     Ftp.cwd (remote path) × enter the target directory
     Remote? File? Names = FTP. Nlst()? Get file list
     ftp.quit()
     if today_file in remote_file_names:
         return True
     else:
         return False
 
 def download_file():
     "" "for target file download" ""
     ftp = ftp_connect()
     bufsize = 1024
     fp = open(local_file, 'wb')
     Ftp.set ﹐ debuglevel (0) ﹐ higher level for easy troubleshooting
     ftp.retrbinary('RETR ' + remote_file, fp.write, bufsize)
     fp.close()
     ftp.quit()
 
 
 while True:
     If int (time. Strftime (""% H% m "")) in range (begin time, end time): 񖓿 judge whether it is in the execution time range
         If int (time. Strftime (""% Y% m% d "") - int (today) = = 0: ා judge whether to cross dates or not
             While not os.path.exists (local? File): 񖓿 judge whether the local file already exists
                 If remote file exists(): ා judge whether the remote file already exists
                     download_file() 
                     with open(log_file, 'a') as f:
                         f. Write ('\ n' + time. Strftime (""% Y /% m /% d% H:% m:% s "") + "today's file has been downloaded! ""
                     Time.sleep (60) ා 1 minute silence after downloading
                 else:
                     time.sleep(180)
                     Break ා note that the cycle will jump out here to judge the date again, so as to avoid falling into the inner cycle when there is no file on the weekend or on the same day
             else:
                 time.sleep(180)
         else:
             "" "if across dates, update each file date based on the current date" ""
             Today = time.strftime (""% Y% m% d "") × date of the day
             Today ﹐ file = today + 'test. TXT' ﹐ get the target file name of today's date
             Remote file = Remote path + today file
             Local file = '\ \ \ \ local \' + today + '\ \' + today file
             with open(log_file, 'a') as f:
                 f. Write ('\ n' + time. Strftime (""% Y /% m /% d% H:% m:% s "") + "task started, file date updated. ""
     else:
         time.sleep(1800)

 

4、 Operation

Save as PYW file, the task runs continuously in the background, no need to plan the task, save effort and effort.

There is no need to download the tag. One is simpler. The other is that if the local file is deleted or moved by mistake, it can be downloaded again automatically.

In the log, only the start of the task and the download flag of the file are written every day, and the corresponding time is recorded, which can be added if necessary.

 

I hope I can help friends in need.

 

Kindly give us your advice!