Python ftplib module to write a simple FTP service

Source: Internet
Author: User

1  fromFtplibImport*2 ImportOs,readline3 ImportSYS4 classmyftp:5FTP = FTP ()#Create a link to an FTP object6      def __init__(Self, host, port=' +'):#Constructor Initialization7Self.ftp.connect (host=host,timeout=100)#connecting to an FTP server8      defLogin (self, User, passwd):#Login Function9Self.ftp.login (User=user, passwd=passwd)Ten          Print(Self.ftp.welcome)#FTP display welcome message after successful login One           A          defDownLoadFile (self, Localfilepath, Remotefilepath): -File = Open (Localfilepath,'WB') -Self.ftp.retrbinary ("RETR%s"%( Remotefilepath), File.write)#download files to local the file.close () -          returnTrue -   -      defDownloadfiletree (self, Localfiledir, Remotefiledir): +          ifOs.path.isdir (localfiledir) = = False:#If the incoming local is not a directory -Os.makedirs (Localfiledir)#Create a new directory locally +SELF.FTP.CWD (Remotefiledir)#switch to remote directory ARemotefiles = Self.ftp.nlst ()#Pass all the files in the remote directory to the remotefiles variable. at           forFileinchRemotefiles: -Local =os.path.join (localfilesdir, file) -Chang =Os.path.join (remotedir,file) -              ifSelf . Isdir (Chang): - Self . Downloadfiletree (Local, Chang) -              Else: in Self . DownLoadFile (Local, Chang) -SELF.FTP.CWD (".." ) to   +      defisdir (self, path): -          ifOs.path.isdir (path) = =True: theSelf.juge =True *          Else: $Self.juge =FalsePanax Notoginseng          returnSelf.juge -                   the      defUploadfiletree (self, Localfiledir, Remotefiledir): +          ifOs.path.isdir (localfiledir) = =False: A              Print('wrong! Please Input Dir') the          ifOs.path.isdir (remotefiledir) = =False: + os.makedirs (Remotefiledir) -Localfiles =Os.listdir (Localfiledir) $ self.ftp.cwd (Remotefiledir) $           forLocalinchLocalfiles: -src =Os.path.join (Localfiledir, Local) -Chang =Os.path.join (remotefiledir,local) the             ifOs.path.isdir (SRC): - Self . Uploadfiletree (Src,chang)Wuyi             Else: the Self . UploadFile (Src,chang) -SELF.FTP.CWD (".." ) Wu                  -      defUploadFile (self, Localfilepath, Remotefilepath): About          ifOs.path.isfile (localfilepath) = =False: $              returnFalse -File = Open (LocalFile,"RB") -Self.ftp.storbinary ('STOR%s'%remotefile, file, 4096) - file.close () A   +   theFTP = MYFTP ('192.168.19.153')#instantiation of -  $ defLogin (): theFtp.login ('Root','Root')         the defUpdate (): the     Print('\033[34muping.....\033[m') theFtp.uploadfiletree ('Main',"/xxx" ) -     Print(' Done') in defDownLoad (): the     Print('\033[34mdowning.....\033[m') theFtp.downloadfiletree ('Localdir','Remotedir') About         Print(' Done') the defClose (): the self.ftp.quit () the  + defMenu (): -     Print("""\033[;32mwelcome \033[0m\n""") the     Print("\ t (1) Login")Bayi     Print("\ t (2) Update") the     Print("\ t (3) DownLoad") the     Print("\ t (4) Close") -      whileTrue: -Choices = Raw_input ('\033[32mchoice>>\033[m'). Strip () the        ifLen (choices) = = 0:Continue the        ifChoices = ='1': Login () the        elifChoices = ='2': Update () the        elifChoices = ='3':D ownload () -        elifChoices = ='4': Close () the         theMenu ()



Python ftplib module to write a simple FTP service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.