Python implementation of FTP file timing automatic download

Posted by thebusinesslad on Mon, 11 Nov 2019 11:00:22 +0100

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 will criticize me more.

 

I. demand:

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.

 

II. Analysis:

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

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

 

2. It is necessary 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. 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.

 

III. code implementation

 1 #!/usr/bin/env python
 2 # _*_ coding:utf-8 _*_
 3 
 4 '''
 5 @Time    : 2019-11-11 13:30
 6 @Author  : Peanut_C
 7 @FileName: ftp_auto_download.py
 8 '''
 9 
10 
11 import time
12 from ftplib import FTP
13 import os
14 
15 
16 remote_path = "/xxx/yy/z/"  # Remote directory
17 begin_time = 1500  # Task start time
18 end_time = 1700  # Task end time
19 
20 
21 today = time.strftime("%Y%m%d")  # Date of the day
22 today_file = today + 'test.txt'  # Get the target file name of the date of the day
23 remote_file = remote_path + today_file  # Remote filename
24 local_file = '\\\\local\\' + today + '\\' + today_file  # Local filename
25 log_file = 'C:\\\\log\\ftp_log.txt'
26 
27 
28 def ftp_connect():
29     """Be used for FTP Connect"""
30     ftp_server = 'w.x.y.z'  # ftp Site corresponding IP address
31     username = 'ftpuser'  # User name
32     password = 'ftppass'  # Password
33     ftp = FTP()
34     ftp.set_debuglevel(0) # Higher level for easy troubleshooting
35     ftp.connect(ftp_server, 21)
36     ftp.login(username, password)
37     return ftp
38 
39 def remote_file_exists():
40     """Be used for FTP Site target file presence detection"""
41     ftp = ftp_connect()
42     ftp.cwd(remote_path) # Enter target directory
43     remote_file_names = ftp.nlst()  # Get file list
44     ftp.quit()
45     if today_file in remote_file_names:
46         return True
47     else:
48         return False
49 
50 def download_file():
51     """For target file download"""
52     ftp = ftp_connect()
53     bufsize = 1024
54     fp = open(local_file, 'wb')
55     ftp.set_debuglevel(0) # Higher level for easy troubleshooting
56     ftp.retrbinary('RETR ' + remote_file, fp.write, bufsize)
57     fp.close()
58     ftp.quit()
59 
60 
61 while True:
62     if int(time.strftime("%H%M")) in range(begin_time, end_time):  # Judge whether it is in the execution time range
63         if int(time.strftime("%Y%m%d")) - int(today) == 0:  # Judge whether to cross date
64             while not os.path.exists(local_file):  # Determine whether the local file already exists
65                 if remote_file_exists():  # Determine whether the remote end has files
66                     download_file() 
67                     with open(log_file, 'a') as f:
68                         f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " Today's file has been downloaded!")
69                     time.sleep(60)  # Download completed, silence for 1 minute
70                 else:
71                     time.sleep(180)
72                     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
73             else:
74                 time.sleep(180)
75         else:
76             """If across dates, update each file date based on the current date"""
77             today = time.strftime("%Y%m%d")  # Date of the day
78             today_file = today + 'test.txt'  # Get the target file name of the date of the day
79             remote_file = remote_path + today_file  # Remote filename
80             local_file = '\\\\local\\' + today + '\\' + today_file  # Local filename
81             with open(log_file, 'a') as f:
82                 f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " Task initiation, File date updated.")
83     else:
84         time.sleep(1800)

 

IV. 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.

 

More advice!

Topics: Python ftp ftplib