خرید بک لینک

Vote count: 0

I'm trying to use ftplib to transfer a set of files from my computer (ruing 64-bit Windows 7) to a Linux server. The process is similar to this snippet of test code (server address, useame, and password changed, obviously):

import ftplib
import os.path
import os

host     = "some.ftp.server.com"
useame = "useame"
password = "password"

outDir = "/some/output/directory"

def transfer_files():
    ftp = ftplib.FTP(host, useame, password)

    ftp.cwd(outDir)
    names = ftp.nlst()
    if "transferred" not in names:
        ftp.mkd("transferred")
    ftp.cwd("transferred")
    names = ftp.nlst()

    # Transfrer arbitrary files to the server
    filesToTransfer = os.listdir('.')
    for fName in filesToTransfer:
        if not os.path.isfile(fName):
            continue
        if fName in names:
            ftp.delete(fName)
        with open(fName, 'r') as f:
            ftp.storbinary("STOR %s" % fName, f)
        print fName

    ftp.quit()

    print "Done"

if __name__ == "__main__":
    transfer_files()

The behavior I'm seeing is that most files transfer quickly and successfully, but randomly a file will instead time out and raise the following exception:

Traceback (most recent call last):
  File "Test.py", line 37, in <module>
    transfer_files()
  File "Test.py", line 29, in transfer_files
    ftp.storbinary("STOR %s" % base, f)
  File "C:Python27Libftplib.py", line 471, in storbinary
    co = self.transfercmd(cmd, rest)
  File "C:Python27Libftplib.py", line 376, in transfercmd
    retu self.ntransfercmd(cmd, rest)[0]
  File "C:Python27Libftplib.py", line 335, in ntransfercmd
    co = socket.create_coection((host, port), self.timeout)
  File "C:Python27Libsocket.py", line 575, in create_coection
    raise err
socket.error: [Ero 10060] A coection attempt failed because the coected party did not properly respond after a period of time, or established coection failed because coected host has failed to respond

The file that times out is different each time I run the program, but it always seems to happen to one file or another. Why could the transfer be randomly timing out, and what can I do to prevent this?

asked 6 mins ago

1 Answer

Vote count: 0

When working with network requests in bulk, it's completely normal to get a certain number of timeouts - it's even expected. Rather than try to prevent timeouts from happening (because they always will), you should add logic to your application to handle cases where timeouts occur. Since you're uploading files in bulk, that probably looks like re-attempting the upload when that exception gets raised.

answered 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 24 خرداد 1395 ساعت: 21:56

صفحه بندی