Python implementation ftp upload download file

Source: Internet
Author: User

#!/usr/bin/env python# encoding: utf-8__author__ =  "Pwy" "Upload: Upload files and back up to other directories to download: Download files, and delete the remote file "' From ftplib import ftpfrom time import sleepimport os,datetime, loggingfrom shutil import movehost =  "192.168.1.221" user =  "Sxit" PASSWORD  =  "1qaz! QAZ "#PORT  = " "#Upload  the file, change the directoryremotedir = " /home/test/"localdir = "/home/sxit/object/"bakdir = "/home/sxit/bak "#Download  the  file, change the directoryRemoredir =  "/home/sxit/object1/" localdir =   "/root/ftp-logging" Logfile = datetime.datetime.now (). Strftime ('%y-%m-%d ') + '. Log ' Logging.basicconfig (level=logging.info,        format= '% (asctime) s  % (filename) s % (levelname) s % (message) s ',         #  datefmt= '%a, %d %b %y %h:%m:%s ',        filename= logfile,         filemode= ' a ') logging. Filehandler (LOGFILE) class class_ftp:    def __init__ (Self,host,user,password, port= ' + '):         self. Host = host        self. User = user        self. Password = password        self. Port = port        self.ftp=ftp ()          self.flag=0     # 0:no connected, 1:  Connting    def connect (self):         try:             if self.flag == 1:                 logging.info ("ftp  has been connected ")              else:                 Self.ftp.connect (self. Host,self. PORT)                  Self.ftp.login (self. User,self. PASSWORD)                 #  SELF.FTP.SET_PASV (False)                  self.ftp.set_debuglevel (0)                  self.flag=1        except exception :             logging.info ("Ftp login failed ")     def up_load (Self,remotedir,localdir,bakdir):         try:             SELF.FTP.CWD (Remotedir)             for  i in os.listdir (Localdir):                 if i.endswith ('. txt '):                     file_handler = open (i, ' RB ')                      self.ftp.storbinary (' stor %s '  % i,file_handler)                      logging.info ("%s  already upload . " %i)                      try:                         if os.path.isdir (Bakdir):                              move (I,bakdir)                               Logging.info ("%S MOVE TO %S ."  %  (I,bakdir)                          else:                             print  "Move the file failed"                               logging.info ("move the %s to %s failed!"% ( I,bakdir))                      except Exception:                         logging.info ("ftp  delete file faild !!!!! ")                      file_handler.close ()             #  self.ftp.quit ()         except exception:  &nbsP;         logging.info ("up_load failed")      def down_load (Self,remoredir,localdir):        try:             SELF.FTP.CWD (Remoredir)              for i in self.ftp.nlst ():                 if i.endswith ('. NET ') :    #match  file                     file_handler = open (i, ' WB ')                       Self.ftp.retrbinary (' retr %s '  % i,file_handler.write)               &nBsp;      logging.info ("%S ALREADY DOWN ." %i)                      try:                         self.ftp.delete (i)                           logging.info ("%s already deleted!" %i)                      except Exception:                         logging.info ("Ftp delete  file faild !!!!! ")                     file_handler.close ()               #self. Ftp.quit ()         except Exception:             logging.info ("Down_load failed") if  __name__ ==  ' __main__ ':     ftp = class_ftp (Host,user,password)     while true:        ftp. Connect ()         # ftp. Down_load (Remoredir,localdir)         ftp. Up_load (Remotedir,localdir,bakdir)         sleep (30)


This article is from the "Python Learning Marping" blog, so be sure to keep this source http://78799999.blog.51cto.com/9500788/1901656

Python implementation ftp upload download file

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.