如何解决列出目录时,捕获ftplib错误不起作用Python 3
我将ftplib与Python 3.6.9结合使用,以列出服务器上的新文件并下载它们。该服务器会定期断开连接,并出现以下两个错误之一:
unset
解决方案似乎很简单,捕获异常,然后重试。不幸的是,我似乎无法捕捉到异常。这很奇怪,因为此解决方案确实可以在下载文件的方法中工作。仅列出目录的方法无法捕获异常。
ConnectionResetError: [Errno 104] Connection reset by peer
ftplib.error_temp: 426 Data connection unexpectedly closed
我收到的错误消息是:
def find_files_and_directories(self,current_path):
for attempt in range(1,self.nr_of_attempts):
try:
return self.connection.mlsd(path=current_path)
except ConnectionResetError as msg:
self.reconnect()
continue
except ftplib.all_errors as msg:
if str(msg)[:3] == '425':
time.sleep(self.wait_time)
continue
else:
self.reconnect()
continue
except ftplib.error_temp as msg:
log.add_message('Exception caught as error_temp',msg)
raise msg
except Exception as e:
log.add_message('Exception was caught by last except in ' +
'find_files_and_directories. That should not ' +
'have happened. ',e)
raise e
else:
self.failure()
这似乎很简单,我想念什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。