2011-08-03 62 views
6

我正在從文件傳輸期間經常超時的片狀FTP服務器下載文件,並且我在想是否有重新連接和恢復下載的方式。我正在使用python的ftplib。下面是我使用的代碼:socket.error:[錯誤110]連接超時超時後恢復FTP下載

任何幫助是極大的讚賞

#! /usr/bin/python 

import ftplib 
import os 
import socket 
import sys 

#--------------------------------# 
# Define parameters for ftp site # 
#--------------------------------# 
site   = 'a.really.unstable.server' 
user   = 'anonymous' 
password  = '[email protected]' 
root_ftp_dir = '/directory1/' 
root_local_dir = '/directory2/' 

#--------------------------------------------------------------- 
# Tuple of order numbers to download. Each web request generates 
# an order numbers 
#--------------------------------------------------------------- 
order_num = ('1','2','3','4') 

#----------------------------------------------------------------# 
# Loop through each order. Connect to server on each loop. There # 
# might be a time out for the connection therefore reconnect for # 
# every new ordernumber           # 
#----------------------------------------------------------------# 
# First change local directory 
os.chdir(root_local_dir) 

# Begin loop through 
for order in order_num: 

    print 'Begin Proccessing order number %s' %order 

    # Connect to FTP site 
    try: 
     ftp = ftplib.FTP(host=site, timeout=1200) 
    except (socket.error, socket.gaierror), e: 
     print 'ERROR: Unable to reach "%s"' %site 
     sys.exit() 

    # Login 
    try: 
     ftp.login(user,password) 
    except ftplib.error_perm: 
     print 'ERROR: Unable to login' 
     ftp.quit() 
     sys.exit() 

    # Change remote directory to location of order 
    try: 
     ftp.cwd(root_ftp_dir+order) 
    except ftplib.error_perm: 
     print 'Unable to CD to "%s"' %(root_ftp_dir+order) 
     sys.exit() 

    # Get a list of files 
    try: 
     filelist = ftp.nlst() 
    except ftplib.error_perm: 
     print 'Unable to get file list from "%s"' %order 
     sys.exit() 

    #---------------------------------# 
    # Loop through files and download # 
    #---------------------------------# 
    for each_file in filelist: 

     file_local = open(each_file,'wb') 

     try: 
      ftp.retrbinary('RETR %s' %each_file, file_local.write) 
      file_local.close() 
     except ftplib.error_perm: 
      print 'ERROR: cannot read file "%s"' %each_file 
      os.unlink(each_file) 

    ftp.quit() 

    print 'Finished Proccessing order number %s' %order 

sys.exit() 

,我得到的錯誤。

+0

絕對檢查http://ftputil.sschwarzer.net/trac,它會使任何FTP相關的任務更容易。 – agf

回答

3

恢復通過FTP僅使用標準的設施(見RFC959)下載需要使用塊的傳輸模式(第3.4.2節),其可使用MODE B命令設置的。雖然這個特性在技術上是符合規範要求的,但我不確定所有的FTP服務器軟件都能實現它。

在塊傳輸模式下,與流傳輸模式相反,服務器以塊爲單位發送文件,每個塊都有一個標記。此標記可能會重新提交給服務器以重新啓動失敗的傳輸(第3.5節)。

規範說:

[...] a restart procedure is provided to protect users from gross system failures (including failures of a host, an FTP-process, or the underlying network).

然而,據我所知,該規範沒有定義的標記所需的壽命。它只是說以下內容:

The marker information has meaning only to the sender, but must consist of printable characters in the default or negotiated language of the control connection (ASCII or EBCDIC). The marker could represent a bit-count, a record-count, or any other information by which a system may identify a data checkpoint. The receiver of data, if it implements the restart procedure, would then mark the corresponding position of this marker in the receiving system, and return this information to the user.

它應該是安全的假設,實施此功能將提供有FTP會話之間的有效指標,但您的里程可能會有所不同的服務器。

0

要做到這一點,你必須保持中斷的下載,然後找出你丟失的文件的哪些部分,下載這些部分,然後將它們連接在一起。我不知道如何做到這一點,但有一個Firefox和Chrome的下載管理器,名爲DownThemAll。雖然代碼不是用python編寫的(我認爲它是JavaScript),但您可以查看代碼並查看它是如何實現的。

DownThemll - http://www.downthemall.net/

+0

DownThemAll用JavaScript和XUL(XML用戶界面語言)編寫。來源 - http://en.wikipedia.org/wiki/DownThemAll!和https://github.com/nmaier/DownThemAll – Neil