[Tutor] Py 2.4.4: Inheriting from ftplib.FTP()
boB Stepp
robertvstepp at gmail.com
Thu Jun 16 11:38:13 EDT 2016
I am extremely gradually trying to dip my big toe into the waters of
writing classes. I have a little bit of extra time at work today, so
I thought I would start writing a class to replace a bunch of shell
FTP scripts I've used in various programs that would be flexible
enough to be reused wherever I need it. I immediately ran into
problems, which I have (to the best of my knowledge) resolved.
However, I am still in the early stages of writing the methods for
this class, so I thought I would ask for any likely "gotchas" that
might come my way.
Python 2.4.4 on Solaris 10:
from ftplib import FTP
# Current values for Windows-based intranet FTP server:
server_ip = 'my_default_IP'
user = 'default_user'
passwd = 'default_pw'
class FTPFiles(FTP, object):
"""FTP files to Windows server location(s)."""
def __init__(self, host=server_ip, user=user, passwd=passwd):
"""Initialize FTPFiles object. Normally the defaults will be used."""
super(FTPFiles, self).__init__(host, user, passwd)
self.host = host
self.user = user
self.passwd = passwd
def print_welcome_msg(self):
"""Print welcome message sent by FTP server."""
print self.getwelcome()
if __name__ == '__main__':
ftp = FTPFiles()
ftp.print_welcome_msg()
ftp.quit()
What I learned today:
1) FTP from ftplib appears to be an old-style class.
2) I can't just use "class FTPFiles(FTP)" or I will be using old-style classes.
3) I need to use "class FTPFiles(FTP, object)" to use new-style
classes and "object" must come AFTER "FTP".
4) I have to use "super(FTPFiles, self).__init__(host, user, passwd)"
or I cannot successfully inherit from the FTP() class. Also, "self"
apparently must come AFTER "FTPFiles" in the super expression.
Questions:
1) Are there any more common "gotchas" that might be coming my way in
trying to use a new-style class inheriting from an old-style class in
ancient Py 2.4.4?
2) I don't have much knowledge about FTP processes. This is being
used on a secure intranet environment. And I am using the ftplib for
the first time. In its docs it mentioned that the quit() method is
the "polite" way to close the connection, but that an exception may be
raised if the responds with an error to the "QUIT" command the method
sends. If this happens, will the connection close? Or should I do
something like:
try:
ftp.quit()
except:
ftp.close()
? It seems here (If I should use the "try" block.) that a bare
"except" is appropriate as I would think I would want to close the
connection regardless of the type of error the "QUIT" command
generates.
3) The point of the print_welcome_msg() method is to be sure that I
have actually successfully connected to the FTP server. Is there a
better way to check for connection success?
4) As this class stuff is mostly treading new ground for me, am I
doing anything that I should not be doing or should do in a more
preferred way? Keep in mind that I am just starting on this code
today.
As always, many thanks in advance!
--
boB
More information about the Tutor
mailing list