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

在 Python ftplib FTP 傳輸文件上傳中處理斷開(kāi)連接

Handling disconnects in Python ftplib FTP transfers file upload(在 Python ftplib FTP 傳輸文件上傳中處理斷開(kāi)連接)
本文介紹了在 Python ftplib FTP 傳輸文件上傳中處理斷開(kāi)連接的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

如何處理 ftplib 中的斷開(kāi)連接?

How can I handle disconnects in ftplib?

我編寫(xiě)了一個(gè) Python 腳本,我將使用它來(lái)使用 ftplib 將非常大的文件上傳到 FTP 服務(wù)器.

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

我的問(wèn)題是:由于文件的大小,上傳可能會(huì)花費(fèi)很多時(shí)間,如果互聯(lián)網(wǎng)在中間斷開(kāi)連接然后在 1 分鐘后重新連接怎么辦?如何在腳本中處理此類問(wèn)題?有什么想法嗎?

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?

我想到的是一個(gè) try except 塊,它不斷檢查互聯(lián)網(wǎng)連接是否可用.有什么想法嗎?

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

謝謝

推薦答案

使用 Python ftplib 上傳時(shí)處理斷開(kāi)連接的簡(jiǎn)單實(shí)現(xiàn):

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)

建議進(jìn)行更細(xì)粒度的異常處理.

類似的下載:
超時(shí)后恢復(fù)FTP下載

這篇關(guān)于在 Python ftplib FTP 傳輸文件上傳中處理斷開(kāi)連接的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開(kāi)頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒(méi)有插入到 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:“浮動(dòng)對(duì)象不可下標(biāo))
主站蜘蛛池模板: 黄色91在线 | 中文字幕电影在线观看 | 福利在线看 | 久久久久国产一区二区三区四区 | 久久久久亚洲精品 | 九九导航 | 欧美日韩亚洲国产综合 | av在线免费播放 | 亚洲精品一区二区在线观看 | 亚洲精品乱码8久久久久久日本 | 精品国产精品国产偷麻豆 | 伊人久久大香线 | 97精品久久 | 亚洲人免费视频 | 91在线观| 精品无码久久久久久国产 | 欧美中文在线 | 久久国内 | 51ⅴ精品国产91久久久久久 | 久久com| 色婷婷av777| 免费观看成人性生生活片 | 一本久久a久久精品亚洲 | 日韩精品视频一区二区三区 | 一区二区国产在线观看 | 久久久一二三区 | 成人欧美一区二区三区在线观看 | 日韩精品一区二区三区视频播放 | 日日射影院 | 欧美99 | 成人小视频在线观看 | 中文字幕黄色大片 | 91在线网站| 国产成人综合在线 | 久亚州在线播放 | 97精品久久 | 欧美一区不卡 | 99精品网 | 97超碰免费 | 国产色网站 | 范冰冰一级做a爰片久久毛片 |