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

(yzsuai) #1
Windows happens when data is written to the file. As discussed earlier, text mode
also means that the file’s write method will allow for the str string passed in by
retrlines, and that text will be encoded per Unicode when written.
Subtly, though, we also explicitly use the FTP connection object’s Unicode encod-
ing scheme for our text output file in open, instead of the default. Without this
encoding option, the script aborted with a UnicodeEncodeError exception for some
files in my site. In retrlines, the FTP object itself reads the remote file data over a
socket with a text-mode file wrapper and an explicit encoding scheme for decoding;
since the FTP object can do no better than this encoding anyhow, we use its en-
coding for our output file as well.
By default, FTP objects use the latin1 scheme for decoding text fetched (as well
as for encoding text sent), but this can be specialized by assigning to their
encoding attribute. Our script’s local text output file will inherit whatever encoding
ftplib uses and so be compatible with the encoded text data that it produces and
passes.
We could try to also catch Unicode exceptions for files outside the Unicode en-
coding used by the FTP object, but exceptions leave the FTP object in an unre-
coverable state in tests I’ve run in Python 3.1. Alternatively, we could use wb binary
mode for the local text output file and manually encode line strings with
line.encode, or simply use retrbinary and binary mode files in all cases, but both
of these would fail to map end-lines portably—the whole point of making text
distinct in this context.

All of this is simpler in action than in words. Here is the command I use to download
my entire book support website from my ISP server account to my Windows laptop
PC, in a single step:


C:\...\PP4E\Internet\Ftp\Mirror> downloadflat.py test
Password for lutz on home.rmi.net:
Clean local directory first? y
connecting...
deleting local 2004-longmont-classes.html
deleting local 2005-longmont-classes.html
deleting local 2006-longmont-classes.html
deleting local about-hopl.html
deleting local about-lp.html
deleting local about-lp2e.html
deleting local about-pp-japan.html

...lines omitted...

downloading 2004-longmont-classes.html to test\2004-longmont-classes.html as text
downloading 2005-longmont-classes.html to test\2005-longmont-classes.html as text
downloading 2006-longmont-classes.html to test\2006-longmont-classes.html as text
downloading about-hopl.html to test\about-hopl.html as text
downloading about-lp.html to test\about-lp.html as text
downloading about-lp2e.html to test\about-lp2e.html as text
downloading about-pp-japan.html to test\about-pp-japan.html as text

878 | Chapter 13: Client-Side Scripting

Free download pdf