cat getlatestftp.py#!/usr/bin/pythonimport ftplibimport osimport sockethost = ' 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 ()
Summarize:
The 1,ftp.mozilla.org is no longer accessible.
2, the relevant variables are modified according to the actual situation, Host,dirn,file.
3, this content is in "Python Core programming (3rd Edition)" 79th page.
Python Core Programming (3rd Edition)-Client FTP program sample