1 #!/usr/bin/env python 2 3 import ftplib 4 import os 5 import socket 6 7 HOST='ftp.mozilla.org' 8 DIRN='pub/mozilla.org/webtools' 9 FILE='bugzilla-LATEST.tar.gz' 10 11 def main(): 12 try: 13 f=ftplib.FTP(HOST) 14 except (socket.error,socket.gaierror),e: 15 print 'ERROR: cannot reach "%s"' % HOST 16 return 17 print '*** Connected to host "%s"' % HOST 18 19 try: 20 f.login() 21 except ftplib.error_prem: 22 print 'ERROR: cannot login anonymously' 23 f.quit() 24 return 25 print '*** Logged in as "anonymous"' 26 27 try: 28 f.cwd(DIRN) 29 except ftplib.error_perm: 30 print 'ERROR: cannot CD to "%s"' % DIRN 31 f.quit() 32 return 33 print '*** Changed to "%s" folder' % DIRN 34 35 try: 36 f.retrbinary('RETR %s' % FILE, 37 open(FILE,'wb').write) 38 except ftp.error_perm: 39 print 'ERROR: cannot read file "%s"' % FILE 40 os.unlink(FILE) 41 else: 42 print '*** Downloaded "%s" to CWD' % FILE 43 f.quit() 44 return 45 46 if __name__=='__main__': 47 main()
欢迎点击http://www.cnblogs.com/X-zone/梦猩魂的个人博客