久久久久久久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:“浮動對象不可下標)
主站蜘蛛池模板: 亚洲欧美日韩精品 | 久久综合九色综合欧美狠狠 | 99re在线视频观看 | 黄色免费在线观看网址 | 午夜视频在线观看视频 | 亚洲一区二区三区免费视频 | a视频在线观看 | 日本黄色不卡视频 | 精品福利视频一区二区三区 | 午夜精品久久久久久久99黑人 | 成人久久视频 | 亚洲成人久久久 | 免费国产一区二区视频 | 91色综合 | 中文字幕第5页 | 国产视频亚洲视频 | 亚洲精品18 | 中文字幕乱码一区二区三区 | 在线2区| 国产精品精品久久久 | 久久机热 | 91美女在线观看 | 日韩精品一区二区三区 | 国产专区在线 | 欧美日韩成人 | 国产精品一区二区不卡 | 黄色片大全在线观看 | 中文字幕一级毛片视频 | 免费视频一区二区 | 日韩a | 成人精品鲁一区一区二区 | a在线视频| 在线观看国产视频 | 欧美日韩中文在线观看 | 二区中文 | 久久久久久久久久久久亚洲 | 激情久久网 | 夜夜爽99久久国产综合精品女不卡 | 一级片在线观看 | 久久久久亚洲精品国产 | 欧美精品一区三区 |