5
將大量文件上傳到FTP服務器。在上傳過程中,服務器超時,導致我無法繼續上傳。有誰知道檢測服務器是否超時,重新連接並繼續傳輸數據的方法嗎?我正在使用Python的ftp庫進行傳輸。如何在Python中檢測ftp服務器超時
感謝
將大量文件上傳到FTP服務器。在上傳過程中,服務器超時,導致我無法繼續上傳。有誰知道檢測服務器是否超時,重新連接並繼續傳輸數據的方法嗎?我正在使用Python的ftp庫進行傳輸。如何在Python中檢測ftp服務器超時
感謝
你可以簡單地指定一個超時的連接,但對於文件傳輸或者它不是那麼簡單了其它操作期間超時。
由於storbinary和retrbinary方法允許您提供回調,因此可以實現看門狗定時器。每次獲取數據時,都會重置計時器。如果您至少每隔30秒(或其他)沒有收到數據,則看門狗將嘗試中止並關閉FTP會話並將事件發送回您的事件循環(或其他)。
ftpc = FTP(myhost, 'ftp', 30)
def timeout():
ftpc.abort() # may not work according to docs
ftpc.close()
eventq.put('Abort event') # or whatever
timerthread = [threading.Timer(30, timeout)]
def callback(data, *args, **kwargs):
eventq.put(('Got data', data)) # or whatever
if timerthread[0] is not None:
timerthread[0].cancel()
timerthread[0] = threading.Timer(30, timeout)
timerthread[0].start()
timerthread[0].start()
ftpc.retrbinary('RETR %s' % (somefile,), callback)
timerthread[0].cancel()
如果這不夠好,看起來你將不得不選擇不同的API。扭曲的框架有FTP protocol support,應該允許你添加超時邏輯。
你會得到什麼樣的迴應(如果有的話)?它是[400個代碼之一](http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes)? –