久久久久久久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:“浮動對象不可下標)
主站蜘蛛池模板: 91福利在线导航 | 午夜av电影院 | 亚洲成人中文字幕 | 亚洲精品乱码久久久久久久久 | 久草欧美视频 | 亚洲国产中文字幕 | 久久久久久久久99 | 国产欧美精品一区二区 | 黄色毛片黄色毛片 | 日韩一区不卡 | 日本一卡精品视频免费 | 超碰人人艹 | 欧美精品中文字幕久久二区 | 国产高清av免费观看 | 在线亚洲免费视频 | 亚洲成人精品一区 | 九九色九九 | 国产欧美一区二区三区在线看 | 欧美激情国产精品 | 亚洲视频在线看 | 殴美黄色录像 | 成人影院免费视频 | 欧美日韩国产三级 | 欧美13videosex性极品 | 精品国产免费一区二区三区五区 | 日韩高清一区 | 日本中文字幕在线观看 | 国产免费av在线 | 日韩视频在线播放 | 日韩在线一区二区 | 黑人巨大精品欧美一区二区一视频 | 最新国产精品视频 | 日本国产精品视频 | 日本电影免费完整观看 | 天天精品在线 | 99精品国产一区二区三区 | 成人在线免费观看av | 欧美黄在线观看 | 天天玩天天操天天干 | 在线午夜 | 免费一区在线观看 |