Python implements a simple ftp client, pythonftp Client
This example describes how to implement a simple ftp client using python. Share it with you for your reference. The specific implementation method is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-import ftplibimport osimport socketHOST = 'ftp .mozilla.org 'dirn = 'pub/mozilla.org/webtools'FILE = comment 'def writedata (data ): f = open (FILE, 'wb ') try: f. write (data) finally: f. close () def main (): try: f = ftplib. FTP (HOST) socket T (socket. error, socket. gaierror): print 'error: cannot reach "% s" '% HOST return print' *** Connected to host "% s" '% HOST try: f. login () Counter t ftplib. error_perm: print 'error: cannot login anonymously 'f. quit () return print '*** Logged in as "anonymously" 'Try: f. cwd (DIRN) effect 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 () it is called every time a binary data is received. 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 () returnif _ name _ = '_ main _': main ()
I hope this article will help you with Python programming.