通过 Python 的 ftplib 传输文件会不确定超时



我正在尝试使用ftplib将一组文件从我的计算机(运行64位Windows 7)传输到Linux服务器。该过程类似于以下测试代码片段(显然,服务器地址,用户名和密码已更改):

import ftplib
import os.path
import os
host     = "some.ftp.server.com"
username = "username"
password = "password"
outDir = "/some/output/directory"
def transfer_files():
    ftp = ftplib.FTP(host, username, 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()

我看到的行为是大多数文件快速成功传输,但随机文件将超时并引发以下异常:

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
    conn = self.transfercmd(cmd, rest)
  File "C:Python27Libftplib.py", line 376, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:Python27Libftplib.py", line 335, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout)
  File "C:Python27Libsocket.py", line 575, in create_connection
    raise err
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

每次运行程序时超时的文件都不同,但似乎总是发生在一个文件或另一个文件上。为什么传输会随机超时,我可以做些什么来防止这种情况?

批量处理网络请求时,获得一定数量的超时是完全正常的 - 甚至是意料之中的。与其试图阻止超时发生(因为它们总是会发生),不如向应用程序添加逻辑来处理发生超时的情况。由于您正在批量上传文件,因此在引发异常时可能看起来像重新尝试上传。

相关内容

  • 没有找到相关文章

最新更新