2011-08-03 53 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