制作一个好的ftp检查python程序有困难
请容忍我。我开始使用python才一个星期。问题是:我想连接到FTP服务器。假定我的ftp和本地目录上的文件结构相同。我希望python程序执行以下操作: 1> 在运行程序时,它应该上载所有不在服务器上但在我的计算机上的文件 localUpload只上载丢失的文件,不替换所有文件。 比如说,我添加了一个新目录或一个新文件,它应该按原样上传到服务器上。 2> 然后,它应该检查本地和服务器上文件的修改时间,并告知哪一次是最新的 现在,我制作了两个程序: 1> 一个将所有文件从本地上传到服务器的程序。我宁愿它检查丢失的文件,然后只加载丢失的文件文件夹。不是全部。 2> 第二个程序将使用os.walk列出本地的所有文件,它将上载服务器上的所有文件,而无需创建正确的目录结构。 将所有文件复制到服务器的根目录。然后它还检查修改的时间 我现在在艾姆斯,试图将这两个模块连接成一个完美的模块,这正是我所希望的。任何人只要能看到这些代码,并尝试将它们连接到我瓦纳纳所做的事情中,都将是完美的。很抱歉这么痛苦 附言:我可能没有用简单的方法做每件事 代码1:制作一个好的ftp检查python程序有困难,python,datetime,ftp,uploading,download,Python,Datetime,Ftp,Uploading,Download,请容忍我。我开始使用python才一个星期。问题是:我想连接到FTP服务器。假定我的ftp和本地目录上的文件结构相同。我希望python程序执行以下操作: 1> 在运行程序时,它应该上载所有不在服务器上但在我的计算机上的文件 localUpload只上载丢失的文件,不替换所有文件。 比如说,我添加了一个新目录或一个新文件,它应该按原样上传到服务器上。 2> 然后,它应该检查本地和服务器上文件的修改时间,并告知哪一次是最新的 现在,我制作了两个程序: 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")
代码2:
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\Test可能有用。谢谢。我一定会看一看的。另外,上面的代码是我自己的,你认为我可以用它们制作一个同步程序。所需的所有功能都没有,只是我无法将它们连接到一个。