2013-02-20 71 views
1

請耐心等待。它只是一個星期,我開始使用python。繼承人問題:我想連接到FTP服務器。假定我的ftp和本地目錄上的文件結構是相同的。我想讓我的python程序執行以下操作:製作一個很好的ftp檢查python程序的麻煩

1>在運行程序時,它應該上傳所有不在服務器上但位於本地 上的文件(只上傳缺失的文件 - 不能全部替換)。 說,我添加一個新的目錄或一個新的文件,它應該照原樣上載到服務器上。 2>然後它應該檢查本地和服務器上這兩個文件的修改時間,並通知哪些是最新的。

現在,我已經做了兩個方案:

將從本地上傳的所有文件服務器上,因爲它是

1>一個程序。我寧願它檢查丟失的文件,然後僅卸載丟失的文件文件夾。 NOt全部取代。 2>第二個程序將使用os.walk列出來自本地的所有文件,並且它將上傳服務器上的所有文件而不創建正確的目錄結構。 所有被複制到服務器的根目錄。然後它還檢查修改的時間。

我現在正在嘗試將這兩個模塊加入到一個完美的模型中,以滿足我的所有需求。任何能夠真正查看這些代碼並嘗試將它們加入我想要做的事情的人都是完美的。對不起,這樣的痛苦! PS:我可能沒有做任何事情的簡單方法!

代碼NO 1:

import sys 

from ftplib import FTP 

import os 

def uploadDir(localdir,ftp): 

    """ 

    for each directory in an entire tree 

    upload simple files, recur into subdirectories 

    """ 

    localfiles = os.listdir(localdir) 

    for localname in localfiles: 

     localpath = os.path.join(localdir, localname) 


     print ('uploading', localpath, 'to', localname) 
     if not os.path.isdir(localpath): 

      os.chdir(localdir) 


      ftp.storbinary('STOR '+localname, open(localname, 'rb')) 



     else: 

      try: 

       ftp.mkd(localname) 

       print ('directory created') 

      except: 

       print ('directory not created') 

      ftp.cwd(localname)    # change remote dir 

      uploadDir(localpath,ftp)     # upload local subdir 

      ftp.cwd('..')     # change back up 

      print ('directory exited') 


def Connect(path): 
    ftp = FTP("127.0.0.1") 
    print ('Logging in.') 
    ftp.login('User', 'Pass') 
    uploadDir(path,ftp) 


Connect("C:\\temp\\NVIDIA\\Test") 

代碼NO2:

import os,pytz,smtplib 
import time 
from ftplib import FTP 
from datetime import datetime,timedelta 
from email.mime.text import MIMEText 



def Connect_FTP(fileName,pathToFile):      path from the local path 
(dir,file) = os.path.split(fileName) 

fo = open("D:\log.txt", "a+")      # LOgging Important Events 
os.chdir(dir) 
ftp = FTP("127.0.0.1") 
print ('Logging in.') 
ftp.login('User', 'Pass') 
l=dir.split(pathToFile) 
print(l[1]) 

if file in ftp.nlst():           
print("file2Check:"+file) 
    fo.write(str(datetime.now())+": File is in the Server. Checking the Versions....\n") 
    Check_Latest(file,fileName,ftp,fo) 
else: 
    print("File is not on the Server. So it is being uploaded!!!") 
    fo.write(str(datetime.now())+": File is NOT in the Server. It is being Uploaded NOW\n") 
    ftp.storbinary('STOR '+file, open(file, 'rb')) 


print("The End") 


def Check_Latest(file2,path_on_local,ftp,fo):        # Function to check the latest file, USING the "MOdified TIme" 
LOcalFile = os.path.getmtime(path_on_local) 
dloc=datetime.fromtimestamp(LOcalFile).strftime("%d %m %Y %H:%M:%S") 
print("Local File:"+dloc) 
localTimestamp=str(time.mktime(datetime.strptime(dloc, "%d %m %Y %H:%M:%S").timetuple())) # Timestamp to compare LOcalTime 


modifiedTime = ftp.sendcmd('MDTM '+file2)        # Using MDTM command to get the MOdified time. 
IST = pytz.timezone('Asia/Kolkata') 
ServTime=datetime.strptime(modifiedTime[4:], "%Y%m%d%H%M%S") 

tz = pytz.timezone("UTC") 
ServTime = tz.localize(ServTime) 
j=str(ServTime.astimezone(IST))          # Changing TimeZone 
g=datetime.strptime(j[:-6],"%Y-%m-%d %H:%M:%S") 
ServerTime = g.strftime('%d %m %Y %H:%M:%S') 
serverTimestamp=str(time.mktime(datetime.strptime(ServerTime, "%d %m %Y %H:%M:%S").timetuple())) # Timestamp to compare Servertime 

print("ServerFile:"+ServerTime) 

if serverTimestamp > localTimestamp: 
    print ("Old Version on The Client. You need to update your copy of the file") 
    fo.write(str(datetime.now())+": Old Version of the file "+file2+" on the Client\n") 
    return 
else: 
    print ("The File on the Server is Outdated!!!New COpy Uploaded") 
    fo.write(str(datetime.now())+": The server has an outdated file: "+file2+". An email is being generated\n") 
    ftp.storbinary('STOR '+file2, open(file2, 'rb')) 

def Connect(pathToFile): 

for path, subdirs, files in os.walk(pathToFile): 
for name in files: 
    j=os.path.join(path, name) 
    print(j) 
    Connect_FTP(j,pathToFile) 

連接( 「C:\ TEMP \ NVIDIA \測試」)

回答

0

可能this script將是有用的。

+0

謝謝你。我一定會看看它。此外,上面的代碼是我自己的,你認爲我可以真正使用它們同步程序。所有需要的功能都只是我無法將它們加入到其中。 – Jino 2013-02-20 07:31:56