【发布时间】:2016-10-14 00:52:32
【问题描述】:
我正在尝试使用 ftplib 将一组文件从我的计算机(运行 64 位 Windows 7)传输到 Linux 服务器。过程类似于这个测试代码的sn-p(服务器地址,用户名和密码显然改变了):
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
每次运行程序时超时的文件都不一样,但似乎总是发生在一个文件或另一个文件上。为什么传输会随机超时,我该怎么做才能防止这种情况发生?
【问题讨论】: