简体   繁体   中英

Recursive method in python

I am trying to send csv files to remote server via ftp but sometimes i get socket error:

Now i want to make function resursive so that if function fails to send file via ftp it should try again. This is what i am doing , how can i upload files successfully even if on first attempt the connection fails.

    def upload(ftp,file_path):
        os.chdir(file_path)
        files=glob.glob('*.csv')
        for filename in files:
            print filename
            ftp.storbinary("STOR " + filename, open(filename, "rb"), 1024)
            time.sleep(1)

   ftp=FTP(ftp_server)
   ftp.login(ftp_user,ftp_password)
   ftp.cwd("/test")

   file_path='/test'
   upload(ftp,file_path)

I am getting the following error:

Traceback (most recent call last):

  File "file_upload.py", line 205, in <module>
    upload(ftp,file_path)
  File "file_upload.py", line 105, in upload
    ftp.storbinary("STOR " + filename, open(filename, "rb"), 1024)
  File "/usr/lib/python2.7/ftplib.py", line 470, in storbinary
    self.voidcmd('TYPE I')
  File "/usr/lib/python2.7/ftplib.py", line 253, in voidcmd
    self.putcmd(cmd)
  File "/usr/lib/python2.7/ftplib.py", line 181, in putcmd
    self.putline(line)
  File "/usr/lib/python2.7/ftplib.py", line 176, in putline
    self.sock.sendall(line)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 104] Connection reset by peer

Use a try/except catching the socket error:

from socket import error

for filename in files:
        try:
            print filename
            ftp.storbinary("STOR " + filename, open(filename, "rb"), 1024)
            time.sleep(1)
        except error as e:
            print(e)

If you want to retry multiple times use an inner loop, breaking if the call was successful or else retrying n times:

  for filename in files:
        for ty in range(n):
            try:
                print filename
                ftp.storbinary("STOR " + filename, open(filename, "rb"), 1024)
                time.sleep(1)
                break
            except error as e:
                print(e)

You can pass n to your function to decide how many retries you want:

def upload(ftp, file_path, n):
    os.chdir(file_path)
    files = glob.glob('*.csv')
    for filename in files:
        for ty in range(n):
            try:
                print filename
                # with will close your file
                with open(filename, "rb") as f:
                    ftp.storbinary("STOR {}".format(filename), f, 1024)
                time.sleep(1)
                break
            except error as e:
                print(e)

You could use a while True loop but that could potentially loop forever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM