[Tutor] Done at Last!
Britt Green
britt_green@hotmail.com
Mon, 26 Mar 2001 11:50:40 -0800
Alright. I'm done with my first program. It works...most of the time! :) I
would really appreciate some feedback on it because I know how ugly it is.
Also, it tends to crash every now and then. I've attatched the error message
Python gives me at the bottom.
Basically, the program goes into our FTP site and checks to be sure that a
restaurant uploaded a backup of its data, and that its size is +/- 15
percent of the average size (anything bigger/smaller means the backup.zip
was probably corrupt.)
How it works is this: the user selects the city to check the backup. The
program opens the folder for that city (ie: ftp.cwd(boston/)). Once in that
folder, it compiles a list of directories that it will go into and check.
Once it has this list, it begins to go into them one by one.
Inside a one of these restaurant folders, it first checks to see if a backup
was done today. It matches the date string (ie: Mar 26). If this datestring
wasn't found for any of the files, no backup was done for that day, and it
gets noted. If the string was found, it then checks to be sure that the file
for that day was a backup (ie: dayofweek.zip). Sometimes people upload
floorplans and whatnot.
If it found a backup for today, it will then check to be sure the size looks
good.
Finally, it prints out a listing of the results for the restaurants.
I know this program is ugly and very amateurish, but hey, its my first one!
If anyone can point out how to improve it, I would be really grateful.
Thanks!
Britt
import string, re, time
from ftplib import FTP
def getDayOfWeek():
#Thanks to Alan Gauld and his book for this
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday',
'saturday', 'sunday']
theTime = time.localtime(time.time() )
DayNum = theTime[6]
return days[DayNum]
#Get the date of the file to look for
def todaysDate():
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']
theTime = time.localtime(time.time() )
month = theTime[1]
month = month - 1
theTime = time.localtime(time.time() )
day = theTime[2]
if day < 10:
theDate = months[month] + ' ' + str(day)
else:
theDate = months[month] + ' ' + str(day)
return theDate
#This function courtesy of Danny Yoo
def getftplist(ftp):
ftp.sendcmd('TYPE A')
conn = ftp.transfercmd('LIST')
resp = conn.makefile('r').read()
conn.close()
ftp.voidresp()
return resp
#This function checks to be sure the file is not an abnormal size
def checkSize(file, x):
validDays = ['monday.zip', 'tuesday.zip', 'wednesday.zip',
'thursday.zip',
'friday.zip', 'saturday.zip', 'sunday.zip', 'Monday.zip',
'Tuesday.zip', 'Wednesday.zip', 'Thursday.zip',
'Friday.zip',
'Saturday.zip', 'Sunday.zip']
myFile = file
theFiles = x
fileSize = ftp.size(myFile)
allTheFiles = []
totalSize = 0
sizeList = []
#This block seeks that the file with today's date fits one in the
#validDays list.
for item in theFiles:
splitItem = string.split(item)
for z in range(len(splitItem)):
if z == 8:
if splitItem[8] in validDays:
allTheFiles.append(splitItem[8])
#Get the total size of all the files in the directory
for file in allTheFiles:
totalSize += ftp.size(file)
#Find the average size of the files in the directory
averageSize = totalSize / len(allTheFiles)
#Is the file too little, too big or just right?
if (averageSize * .85) > fileSize:
sizeList = [1, fileSize, averageSize]
return sizeList
elif (averageSize * 1.15) < fileSize:
sizeList = [2, fileSize, averageSize]
return sizeList
else:
sizeList = [3]
return sizeList
#As its name implies, this function checks to see if the file found has a
valid
#size. Files +/- 15% will be flagged.
#Some bitch capitlized the first letter of some of the files just to break
#my code!
def getsTodaysFile(today, dirListing):
todaysFile = getDayOfWeek()
todaysFile = todaysFile + ".zip"
dateString = today
theFiles = string.split(dirListing, '\n')
filesToSize = []
#This routine extracts the dir listing with today's date on it
for x in theFiles:
if re.search(dateString, x):
filesToSize.append(x)
#Sometimes there will be more than one file for today. Lets get
#the correct one and size it
for item in filesToSize:
y = string.split(item)
for z in range(len(y)):
if z == 8:
if string.lower(y[8]) == todaysFile:
correctFileName = y[8]
rv = checkSize(correctFileName, theFiles)
return rv
def makeChoice():
choices = ['atl/', 'austin/', 'boston/', 'chicago/', 'dallas/', 'dc/',
'galveston/', 'houston/', 'la/', 'miami/', 'nola/', 'ny/',
'phl/', 'portland/', 'sd/', 'seattle/', 'sf/', 'slc/', 'Vail/']
print """
Which cities would you like to check?
1) Atlanta
2) Boston
3) Chicago
4) DC
5) Los Angeles
6) Miami
7) New Orleans
8) New York
9) Philidelphia
10) Portland and Vail
11) San Diego
12) Seattle
13) San Francisco
14) Salt Lake City
15) Texas
16) All
"""
try:
answer = input("-->")
if answer == 1:
ftpdirs = ['atl/']
return ftpdirs
elif answer == 2:
ftpdirs = ['boston/']
return ftpdirs
elif answer == 3:
ftpdirs = ['chicago/']
return ftpdirs
elif answer == 4:
ftpdirs = ['dc/']
return ftpdirs
elif answer == 5:
ftpdirs = ['la/']
return ftpdirs
elif answer == 6:
ftpdirs = ['miami/']
return ftpdirs
elif answer == 7:
ftpdirs = ['nola/']
return ftpdirs
elif answer == 8:
ftpdirs = ['ny/']
return ftpdirs
elif answer == 9:
ftpdirs = ['phl/']
return ftpdirs
elif answer == 10:
ftpdirs = ['portland/', 'Vail/']
return ftpdirs
elif answer == 11:
ftpdirs = ['sd/']
return ftpdirs
elif answer == 12:
ftpdirs = ['seattle/']
return ftpdirs
elif answer == 13:
ftpdirs = ['sf/']
return ftpdirs
elif answer == 14:
ftpdirs = ['slc/']
return ftpdirs
elif answer == 15:
ftpdirs = ['austin/', 'dallas/', 'galveston/', 'houston/']
return ftpdirs
elif answer == 16:
ftpdirs = choices
return ftpdirs
else:
print "Lets choose a number in the menu, sparky!"
makeChoice()
except (NameError):
print "NameError"
makeChoice()
if __name__ == '__main__':
dirList = makeChoice()
dirsToCheck = []
dirsWithNoFiles = []
dirTooSmall = []
dirTooBig = []
fileJustRight = []
today = todaysDate()
fileIntegrity = []
ftp = FTP('ftp.opentable.com', 'account', 'password')
#This block comes up with a list of directories to enter from the root
dir
for element in dirList:
print "Getting a list of directories to check for " + element
ftp.cwd(element)
dirListing = getftplist(ftp)
splitListing = string.split(dirListing, '\n')
for item in splitListing:
moreSplit = string.split(item)
dirsToCheck.append(element + string.join(moreSplit[8:]))
print dirsToCheck
ftp.cwd('..')
for directory in dirsToCheck:
ftp.cwd(directory)
dirListing = getftplist(ftp)
#This checks to see if a file with today's date is in the directory
m = re.search(today, dirListing)
if m: #If a file with todays date is found:
print directory + " backed up today."
fileIntegrity = getsTodaysFile(today, dirListing)
if fileIntegrity == [3]:
fileJustRight.append(directory)
else:
if fileIntegrity != None:
if fileIntegrity[0] == 1:
small = directory + '\t\t' + str(fileIntegrity[1]) +
'\t' + str(fileIntegrity[2])
dirTooSmall.append(small)
else:
big = directory + '\t\t' + str(fileIntegrity[1]) +
'\t' + str(fileIntegrity[2])
dirTooBig.append(big)
else:#A file with today's date was not found in this directory.
print "No file for today in " + directory
dirsWithNoFiles.append(directory)
ftp.cwd('/')
print "\n" + "Done checking files." + "\n"
print "These directories did not have a backup today."
for i in dirsWithNoFiles:
print i
print "\n"
print "The files in these directories look a bit small."
for z in dirTooSmall:
print z
print "\n"
print "The files in these directories look a bit big."
for k in dirTooBig:
print k
print "\n"
ftp.quit()
#End of program
All works good, except I get this error message pretty regularly. I can't
seem to figure out whats causing it. More than anything else, I'd love a
solution to this difficulty.
Traceback (innermost last):
File "C:/Python20/Code/Copy of dirs.py", line 220, in ?
dirListing = getftplist(ftp)
File "C:/Python20/Code/Copy of dirs.py", line 36, in getftplist
conn = ftp.transfercmd('LIST')
File "c:\python20\lib\ftplib.py", line 297, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "c:\python20\lib\ftplib.py", line 283, in ntransfercmd
sock = self.makeport()
File "c:\python20\lib\ftplib.py", line 254, in makeport
resp = self.sendport(host, port)
File "c:\python20\lib\ftplib.py", line 244, in sendport
return self.voidcmd(cmd)
File "c:\python20\lib\ftplib.py", line 234, in voidcmd
return self.voidresp()
File "c:\python20\lib\ftplib.py", line 209, in voidresp
resp = self.getresp()
File "c:\python20\lib\ftplib.py", line 195, in getresp
resp = self.getmultiline()
File "c:\python20\lib\ftplib.py", line 181, in getmultiline
line = self.getline()
File "c:\python20\lib\ftplib.py", line 168, in getline
line = self.file.readline()
File "c:\python20\lib\socket.py", line 221, in readline
new = self._sock.recv(self._rbufsize)
error: (10054, 'Connection reset by peer')
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com