欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的步驟

 更新時(shí)間:2020年12月19日 10:41:43   作者:Peanut_C  
這篇文章主要介紹了Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

  之前遇到技術(shù)問(wèn)題總能在技術(shù)博客上得到啟發(fā),十分感謝各位的無(wú)私分享。而自己卻很少發(fā)文,固然是水平有限,但也限制了知識(shí)積累和總結(jié)。今后多總結(jié)分享,回饋博客的同時(shí)也希望大家多多批評(píng)。

一、需求:

  某數(shù)據(jù)公司每日15:00~17:00之間,在其FTP發(fā)布當(dāng)日數(shù)據(jù)供下載,我方需及時(shí)下載當(dāng)日數(shù)據(jù)至指定本地目錄。

二、分析:

  1、需實(shí)現(xiàn)FTP登陸、查詢(xún)、下載功能;

  解答:使用內(nèi)置的ftplib模塊中FTP類(lèi);

  2、需判斷文件是否下載;

  解答:使用os模塊中path.exists方法;

  3、需判斷在指定時(shí)間段內(nèi)才執(zhí)行下載任務(wù);

  解答:使用內(nèi)置的time模塊抓取當(dāng)前時(shí)間,并與指定時(shí)間做比較;

  4、需考慮日期切換問(wèn)題;

  解答:使用內(nèi)置的time模塊抓取當(dāng)前日期,并與變量中的日期做比較。

三、代碼實(shí)現(xiàn)

#!/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/" # 遠(yuǎn)端目錄
begin_time = 1500 # 任務(wù)開(kāi)始時(shí)間
end_time = 1700 # 任務(wù)結(jié)束時(shí)間


today = time.strftime("%Y%m%d") # 當(dāng)天日期
today_file = today + 'test.txt' # 得到當(dāng)天日期的目標(biāo)文件名
remote_file = remote_path + today_file # 遠(yuǎn)端文件名
local_file = '\\\\local\\' + today + '\\' + today_file # 本地文件名
log_file = 'C:\\\\log\\ftp_log.txt'


def ftp_connect():
  """用于FTP連接"""
  ftp_server = 'w.x.y.z' # ftp站點(diǎn)對(duì)應(yīng)的IP地址
  username = 'ftpuser' # 用戶(hù)名
  password = 'ftppass' # 密碼
  ftp = FTP()
  ftp.set_debuglevel(0) # 較高的級(jí)別方便排查問(wèn)題
  ftp.connect(ftp_server, 21)
  ftp.login(username, password)
  return ftp

def remote_file_exists():
  """用于FTP站點(diǎn)目標(biāo)文件存在檢測(cè)"""
  ftp = ftp_connect()
  ftp.cwd(remote_path) # 進(jìn)入目標(biāo)目錄
  remote_file_names = ftp.nlst() # 獲取文件列表
  ftp.quit()
  if today_file in remote_file_names:
    return True
  else:
    return False

def download_file():
  """用于目標(biāo)文件下載"""
  ftp = ftp_connect()
  bufsize = 1024
  fp = open(local_file, 'wb')
  ftp.set_debuglevel(0) # 較高的級(jí)別方便排查問(wèn)題
  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): # 判斷是否在執(zhí)行時(shí)間范圍
    if int(time.strftime("%Y%m%d")) - int(today) == 0: # 判斷是否跨日期
      while not os.path.exists(local_file): # 判斷本地是否已有文件
        if remote_file_exists(): # 判斷遠(yuǎn)端是否已有文件
          download_file()
          with open(log_file, 'a') as f:
            f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " 今日文件已下載!")
          time.sleep(60) # 下載完畢靜默1分鐘
        else:
          time.sleep(180)
          break # 注意,此處跳出循環(huán)重新判斷日期,避免周末或當(dāng)天沒(méi)文件時(shí)陷入內(nèi)層循環(huán)
      else:
        time.sleep(180)
    else:
      """如果跨日期,則根據(jù)當(dāng)前日期,更新各文件日期"""
      today = time.strftime("%Y%m%d") # 當(dāng)天日期
      today_file = today + 'test.txt' # 得到當(dāng)天日期的目標(biāo)文件名
      remote_file = remote_path + today_file # 遠(yuǎn)端文件名
      local_file = '\\\\local\\' + today + '\\' + today_file # 本地文件名
      with open(log_file, 'a') as f:
        f.write('\n' + time.strftime("%Y/%m/%d %H:%M:%S") + " 任務(wù)啟動(dòng), 文件日期已更新。")
  else:
    time.sleep(1800)

四、運(yùn)行情況

  保存為pyw文件,任務(wù)在后臺(tái)持續(xù)運(yùn)行,不需要計(jì)劃任務(wù),省心省力。

  不用下載標(biāo)記,一則較為簡(jiǎn)潔,二則本地文件如果被人誤刪或移動(dòng)可自動(dòng)重新下載。

  日志中,每天僅寫(xiě)入任務(wù)啟動(dòng)和文件已下載標(biāo)志,并記錄對(duì)應(yīng)時(shí)間,如有需要可再添加。

  希望能幫到有需要的朋友。

  多多指教!

以上就是Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的步驟的詳細(xì)內(nèi)容,更多關(guān)于python ftp文件定時(shí)下載的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論