我尝试使用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:\Python27\Lib\ftplib.py", line 471, in storbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\Lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\Lib\ftplib.py", line 335, in ntransfercmd
conn = socket.create_connection((host, port), self.timeout)
File "C:\Python27\Lib\socket.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
每次运行程序时超时的文件都不同,但似乎总是发生在一个文件或另一个文件中。为什么转移会随机超时,我该怎么做才能防止这种情况发生呢?
答案 0 :(得分:0)
批量处理网络请求时,获得一定数量的超时是完全正常的 - 甚至可以预期。您应该为应用程序添加逻辑以处理发生超时的情况,而不是试图防止超时发生(因为它们总是会发生)。由于您是批量上传文件,这可能看起来像是在引发异常时重新尝试上传。