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

(yzsuai) #1

first change to the local directory typed into the form so that the fetched file is stored
there (getfile stores in the current working directory, whatever that may be when it is
called). Here are the messages printed in the client’s console, along with a check on the
file transfer; the server is still running above testdir, but the client stores the file else-
where after it’s fetched on the socket:


C:\...\Internet\Sockets> getfilegui.py
Local Dir? => C:\users\Mark\temp
File Name => testdir\ora-lp4e.gif
Server Name => localhost
Port Number => 50001
Client got testdir\ora-lp4e.gif at Sun Apr 25 17:22:39 2010

C:\...\Internet\Sockets> fc /B C:\Users\mark\temp\ora-lp4e.gif testdir\ora-lp4e.gif
FC: no differences encountered

As usual, we can use this interface to connect to servers running locally on the same
machine (as done here), or remotely on a different computer. Use a different server
name and file paths if you’re running the server on a remote machine; the magic of
sockets make this all “just work” in either local or remote modes.


One caveat worth pointing out here: the GUI is essentially dead while the download is
in progress (even screen redraws aren’t handled—try covering and uncovering the
window and you’ll see what I mean). We could make this better by running the down-
load in a thread, but since we’ll see how to do that in the next chapter when we explore
the FTP protocol, you should consider this problem a preview.


In closing, a few final notes: first, I should point out that the scripts in this chapter use
tkinter techniques we’ve seen before and won’t go into here in the interest of space; be
sure to see the GUI chapters in this book for implementation hints.


Keep in mind, too, that these interfaces just add a GUI on top of the existing script to
reuse its code; any command-line tool can be easily GUI-ified in this way to make it
more appealing and user friendly. In Chapter 14, for example, we’ll meet a more useful
client-side tkinter user interface for reading and sending email over sockets (PyMail-
GUI), which largely just adds a GUI to mail-processing tools. Generally speaking, GUIs
can often be added as almost an afterthought to a program. Although the degree of
user-interface and core logic separation varies per program, keeping the two distinct
makes it easier to focus on each.


And finally, now that I’ve shown you how to build user interfaces on top of this chap-
ter’s getfile, I should also say that they aren’t really as useful as they might seem. In
particular, getfile clients can talk only to machines that are running a getfile server.
In the next chapter, we’ll discover another way to download files—FTP—which also
runs on sockets but provides a higher-level interface and is available as a standard
service on many machines on the Net. We don’t generally need to start up a custom
server to transfer files over FTP, the way we do with getfile. In fact, the user-interface
scripts in this chapter could be easily changed to fetch the desired file with Python’s


850 | Chapter 12: Network Scripting

Free download pdf