cat getLatestFTP.py #!/usr/bin/python import ftplib import os import socket HOST = ‘ftp.mozilla.org‘ DIRN = ‘pub/mozilla.org/webtools‘ FILE = ‘bugzilla-LATEST.tar.gz‘ def main(): try: f = ftplib.FTP(HOST) except (socket.error,socket.gaierror) as e: print ‘ERROR:cannot reach "%s"‘ % HOST return print ‘*** Connected to host "%s"‘ % HOST try: f.login(‘web2‘,‘she3t6net6hv3‘) except ftplib.error_perm: print ‘ERROR: cannot login anonymously‘ f.quit() return print ‘*** Logged in as "anonymous"‘ try: f.cwd(DIRN) except ftplib.error_perm: print ‘ERROR:cannot CD to "%s"‘ % DIRN f.quit() return print ‘*** Changed to "%s" folder‘ % DIRN try: f.retrbinary(‘RETR %s‘ % FILE, open(FILE,‘wb‘).write) except ftplib.error_perm: print ‘ERROR:cannot read file "%s"‘ % FILE if os.path.exists(FILE): os.unlink(FILE) else: print ‘*** Downloaded "%s" to CWD‘ % FILE f.quit() if __name__ == ‘__main__‘: main()
总结:
1,ftp.mozilla.org 已经不能访问了。
2,相关变量根据实际情况修改,HOST,DIRN,FILE。
3,该内容在 《Python核心编程(第3版)》 第79页。
原文地址:http://hunkz.blog.51cto.com/6157447/1874384