Steps for Python to Realize Timing Automatic Download of FTP File
- 2021-08-28 20:33:12
- OfStack
I can always get inspiration from technical blog when I encounter technical problems before. Thank you for your selfless sharing. However, I seldom publish articles, which is limited in level, but it also limits the accumulation and summary of knowledge. In the future, we will summarize and share more, give back to the blog, and hope that everyone will criticize more.
1. Requirements:
A data company publishes the data of the day for download in its FTP between 15:00 and 17:00 every day. We need to download the data of the day to the designated local directory in time.
2. Analysis:
1. It is necessary to realize FTP login, query and download functions;
Answer: Use the FTP class in the built-in ftplib module;
2. It is necessary to judge whether the file is downloaded;
Answer: Use path. exists method in os module;
3. It is necessary to judge that the download task is executed within the specified time period;
Answer: Use the built-in time module to capture 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.
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 the current day
remote_file = remote_path + today_file # Remote file name
local_file = '\\\\local\\' + today + '\\' + today_file # Local file name
log_file = 'C:\\\\log\\ftp_log.txt'
def ftp_connect():
""" Used for FTP Connect """
ftp_server = 'w.x.y.z' # ftp Site-corresponding IP Address
username = 'ftpuser' # User name
password = 'ftppass' # Password
ftp = FTP()
ftp.set_debuglevel(0) # Higher levels facilitate troubleshooting problems
ftp.connect(ftp_server, 21)
ftp.login(username, password)
return ftp
def remote_file_exists():
""" Used for FTP Site Object File Existence Detection """
ftp = ftp_connect()
ftp.cwd(remote_path) # Enter the target directory
remote_file_names = ftp.nlst() # Get a list of files
ftp.quit()
if today_file in remote_file_names:
return True
else:
return False
def download_file():
""" Used for destination file download """
ftp = ftp_connect()
bufsize = 1024
fp = open(local_file, 'wb')
ftp.set_debuglevel(0) # Higher levels facilitate troubleshooting problems
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): # Determining whether it is in the execution time range
if int(time.strftime("%Y%m%d")) - int(today) == 0: # Determine whether to cross dates
while not os.path.exists(local_file): # Determine whether there is a file locally
if remote_file_exists(): # Determine whether there is a file at the remote end
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) # Silence after downloading 1 Minutes
else:
time.sleep(180)
break # Note that the loop jumps out here to re-judge the date, so as to avoid falling into an inner loop when there are no files on weekends or days
else:
time.sleep(180)
else:
""" If across dates, update each file date according to the current date """
today = time.strftime("%Y%m%d") # Date of the day
today_file = today + 'test.txt' # Get the target file name of the current day
remote_file = remote_path + today_file # Remote file name
local_file = '\\\\local\\' + today + '\\' + today_file # Local file name
with open(log_file, 'a') as f:
f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " Task initiation , The file date has been updated. ")
else:
time.sleep(1800)
4. Operational status
Save as pyw file, the task runs continuously in the background, and there is no need to plan the task, which saves worry and effort.
No download mark, 1 is more concise, 2 local files if deleted or moved by mistake can be automatically re-downloaded.
In the log, only write the flag of task start and file download every day, and record the corresponding time, and add it again if necessary.
I hope I can help friends in need.
Give me a lot of advice!
The above is the Python implementation of FTP file timing automatic download steps details, more about python ftp file timing download information please pay attention to this site other related articles!