#-*-coding:utf-8-*-#python#Xiaodeng#Python module ftplib (Implementation of FTP upload download code)#Requirements: Implement FTP upload download code (without error handling) fromFtplibImportFTPdefftpconnect (): Ftp_server='ftp.python.org'FTP=FTP () ftp.set_debuglevel (2)#turn on the mode level 2Ftp.connect (ftp_server,21) Ftp.login ("',"')#Username,password returnFTP#implementation of the downloaddefdownloadfile (): Path='/home/static/test.jpeg'#view the path to the file you want to downloadftp=Ftpconnect ()Printftp.getwelcome () bufsize=1024#Set buffer block sizeLocalpath='D:\test2\dog.jpeg'#where to download the fileFp=open (LocalPath,'WB') #note the space behind the RETRFtp.retrbinary ('RETR'+path,fp.write,bufsize)#receive files on the server and write to localftp.set_debuglevel (0) Fp.close () ftp.quit ()#implementation of uploadsdefuploadfile (): Path='/home/static/test.jpeg'FTP=ftpconnect () bufsize=1024LocalPath='D:\test2\dog.jpeg'FP=open (LocalPath,'RB') #note the space behind the StorFtp.storbinary ('STOR'+path,fp,bufsize)#Uploading Filesfp.close () ftp.quit ( )if __name__=="__main__": FTP= Ftpconnect ("',"',"') DownloadFile (FTP,"',"') UploadFile (FTP,"',"') Ftp.quit ()#from: tianzhu123
Python Module ftplib (Implementation of FTP upload download code)