在 Python ftplib FTP 传输文件上传中处理断开连接

Handling disconnects in Python ftplib FTP transfers file upload(在 Python ftplib FTP 传输文件上传中处理断开连接)
本文介绍了在 Python ftplib FTP 传输文件上传中处理断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何处理 ftplib 中的断开连接?

How can I handle disconnects in ftplib?

我编写了一个 Python 脚本,我将使用它来使用 ftplib 将非常大的文件上传到 FTP 服务器.

I wrote a Python scrip that I will use in order to upload very big files to an FTP server using ftplib.

我的问题是:由于文件的大小,上传可能会花费很多时间,如果互联网在中间断开连接然后在 1 分钟后重新连接怎么办?如何在脚本中处理此类问题?有什么想法吗?

My question is: Seeing as upload will probably take a lot of time due to the file's size, what if the internet disconnects in the middle and then reconnects say after 1 minute? How can I handle such issue in the script? Any ideas?

我想到的是一个 try except 块,它不断检查互联网连接是否可用.有什么想法吗?

What I thought about is a try except block that keeps checking if internet connection is available. Any ideas?

谢谢

推荐答案

使用 Python ftplib 上传时处理断开连接的简单实现:

A simple implementation for handling of disconnects while uploading with Python ftplib:

finished = False

local_path = "/local/source/path/file.zip"
remote_path = "/remote/desti/path/file.zip"

with open(local_path, 'rb') as f:
    while (not finished):
        try:
            if ftp is None:
                print("Connecting...")
                ftp = FTP(host, user, passwd)

            if f.tell() > 0:
                rest = ftp.size(remote_path)
                print(f"Resuming transfer from {rest}...")
                f.seek(rest)
            else:
                print("Starting from the beginning...")
                rest = None
            ftp.storbinary(f"STOR {remote_path}", f, rest=rest)
            print("Done")
            finished = True
        except Exception as e:
            ftp = None
            sec = 5
            print(f"Transfer failed: {e}, will retry in {sec} seconds...")
            time.sleep(sec)

建议进行更细粒度的异常处理.

类似的下载:
超时后恢复FTP下载

这篇关于在 Python ftplib FTP 传输文件上传中处理断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)