久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 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 傳輸文件上傳中處理斷開連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 亚洲网在线 | 欧美成人免费视频 | 国产小视频网站 | 国产尤物视频 | 成人毛片一区二区三区 | 久久福利视频导航 | 日皮视频免费看 | 综合一区二区三区 | 在线色网站 | 在线观看av不卡 | 538精品视频 | 久久精品国产视频 | 国产一级片免费看 | 国产欧美日韩综合精品 | 毛片毛片毛片 | 国产福利91精品一区二区三区 | 黄色一极片 | 日日日操操操 | 亚洲视频一区在线观看 | 欧美精品一区二区三区四区 | 自拍视频一区 | 在线观看亚洲一区 | 国产在线二区 | 国产精品一区二区免费 | 天天碰天天操 | 久久av资源 | 国产福利视频在线 | 日韩欧美在线视频 | 中文字幕永久免费 | 国产高潮在线观看 | 欧美国产一区二区 | 国产中文在线 | 久久久网| 欧美精品www | 成人在线视频播放 | 黄色av免费 | 国产成人a亚洲精品 | 国产亚洲视频在线观看 | 亚洲资源网 | 岛国av在线免费观看 | 久久都是精品 |