[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

We learned about ways to work around the no-GUI rule for threads in Chapter 10, and
we will apply such techniques when we explore the PyMailGUI example in the next
chapter. To be portable, though, we can’t really close the GUI until the active-thread
count falls to zero; the exit model of the threading module of Chapter 5 can be used
to achieve the same effect. Here is the sort of output that appears in the console window
when two downloads overlap in time:


C:\...\PP4E\Internet\Ftp> python getfilegui.py
Server Name => ftp.rmi.net
User Name? => lutz
Local Dir => C:\temp
File Name => spain08.JPG
Password? => xxxxxxxx
Remote Dir =>.

Server Name => ftp.rmi.net
User Name? => lutz
Local Dir => C:\temp
File Name => index.html
Password? => xxxxxxxx
Remote Dir =>.

Download of "index.html" successful
Download of "spain08.JPG" successful

This example isn’t much more useful than a command line-based tool, of course, but
it can be easily modified by changing its Python code, and it provides enough of a GUI
to qualify as a simple, first-cut FTP user interface. Moreover, because this GUI runs
downloads in Python threads, more than one can be run at the same time from this
GUI without having to start or restart a different FTP client tool.


While we’re in a GUI mood, let’s add a simple interface to the putfile utility, too. The
script in Example 13-8 creates a dialog that starts uploads in threads, using core FTP
logic imported from Example 13-5. It’s almost the same as the getfile GUI we just
wrote, so there’s not much new to say. In fact, because get and put operations are so
similar from an interface perspective, most of the get form’s logic was deliberately
factored out into a single generic class (FtpForm), so changes need be made in only a
single place. That is, the put GUI here is mostly just a reuse of the get GUI, with distinct
output labels and transfer methods. It’s in a file by itself, though, to make it easy to
launch as a standalone program.


Example 13-8. PP4E\Internet\Ftp\putfilegui.py


"""
###############################################################
launch FTP putfile function with a reusable form GUI class;
see getfilegui for notes: most of the same caveats apply;
the get and put forms have been factored into a single
class such that changes need be made in only one place;
###############################################################
"""


Transferring Files with ftplib | 871
Free download pdf