#! /Usr/bin/python #-*-Coding: UTF-8 -*- Import ftplib Import OS Import socket HOST = 'ftp .w.illa.org' DIRN = 'pub/mozilla.org/webtools' FILE = 'bugzilla-3.6.9-to-3.6.10-nodocs.diff.gz' Def writedata (data ): F = open (FILE, 'wb ') Try: F. write (data) Finally: F. close () Def main (): Try: F = ftplib. FTP (HOST) Handle T (socket. error, socket. gaierror ): Print 'error: cannot reach "% s" '% HOST Return Print '*** Connected to host "% s"' % HOST Try: F. login () Failed t ftplib. error_perm: Print 'error: cannot login anonymously' F. quit () Return Print *** Logged in as "anonymously "' Try: F. cwd (DIRN) Failed t ftplib. error_perm: Print 'errorl cannot CD to "% s" '% DIRN F. quit () Return Print *** Changed to "% s" folder '% DIRN Try: # Pass a callback function to retrbinary (), which is called every time a binary data is received F. retrbinary ('retr % s' % FILE, writedata) Failed t ftplib. error_perm: Print 'error: cannot read file "% s" '% FILE OS. unlink (FILE) Else: Print *** Downloaded "% s" to CWD '% FILE F. quit () Return If _ name _ = '_ main __': Main () |