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

(yzsuai) #1

much of the same code by inheritance), but notice that it traverses and uploads nested
subdirectories along the way:


C:\...\PP4E\Internet\Ftp\Mirror> uploadall.py Website-Training
Password for lutz on learning-python.com:
connecting...
uploading Website-Training\2009-public-classes.htm to 2009-public-classes.htm text
uploading Website-Training\2010-public-classes.html to 2010-public-classes.html text
uploading Website-Training\about.html to about.html text
uploading Website-Training\books to books directory created
uploading Website-Training\books\index.htm to index.htm text
uploading Website-Training\books\index.html to index.html text
uploading Website-Training\books\_vti_cnf to _vti_cnf directory created
uploading Website-Training\books\_vti_cnf\index.htm to index.htm text
uploading Website-Training\books\_vti_cnf\index.html to index.html text
directory exited
directory exited
uploading Website-Training\calendar.html to calendar.html text
uploading Website-Training\contacts.html to contacts.html text
uploading Website-Training\estes-nov06.htm to estes-nov06.htm text
uploading Website-Training\formalbio.html to formalbio.html text
uploading Website-Training\fulloutline.html to fulloutline.html text

...lines omitted...

uploading Website-Training\_vti_pvt\writeto.cnf to writeto.cnf?
uploading Website-Training\_vti_pvt\_vti_cnf to _vti_cnf directory created
uploading Website-Training\_vti_pvt\_vti_cnf\_x_todo.htm to _x_todo.htm text
uploading Website-Training\_vti_pvt\_vti_cnf\_x_todoh.htm to _x_todoh.htm text
directory exited
uploading Website-Training\_vti_pvt\_x_todo.htm to _x_todo.htm text
uploading Website-Training\_vti_pvt\_x_todoh.htm to _x_todoh.htm text
directory exited
Done: 366 files and 18 directories uploaded.

As is, the script of Example 13-15 handles only directory tree uploads; recursive uploads
are generally more useful than recursive downloads if you maintain your websites on
your local PC and upload to a server periodically, as I do. To also download (mirror) a
website that has subdirectories, a script must parse the output of a remote listing com-
mand to detect remote directories. For the same reason, the recursive upload script was
not coded to support the remote directory tree cleanup option of the original—such a
feature would require parsing remote listings as well. The next section shows how.


Deleting Remote Trees


One last example of code reuse at work: when I initially tested the prior section’s
upload-all script, it contained a bug that caused it to fall into an infinite recursion loop,
and keep copying the full site into new subdirectories, over and over, until the FTP
server kicked me off (not an intended feature of the program!). In fact, the upload got
13 levels deep before being killed by the server; it effectively locked my site until the
mess could be repaired.


Transferring Directory Trees with ftplib | 895
Free download pdf