超时后恢复FTP下载 [英] Resume FTP download after timeout

查看:68
本文介绍了超时后恢复FTP下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从不稳定的FTP服务器下载文件,而该FTP服务器在文件传输期间经常超时,我想知道是否存在重新连接并恢复下载的方法.我正在使用Python的ftplib.这是我正在使用的代码:

I'm downloading files from a flaky FTP server that often times out during file transfer and I was wondering if there was a way to reconnect and resume the download. I'm using Python's ftplib. Here is the code that I am using:

#! /usr/bin/python

import ftplib
import os
import socket
import sys

#--------------------------------#
# Define parameters for ftp site #
#--------------------------------#
site           = 'a.really.unstable.server'
user           = 'anonymous'
password       = 'someperson@somewhere.edu'
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()

我得到的错误:

socket.error:[Errno 110]连接超时

socket.error: [Errno 110] Connection timed out

非常感谢您的帮助.

推荐答案

仅使用标准功能通过FTP恢复下载(请参见

Resuming a download through FTP using only standard facilities (see RFC959) requires use of the block transmission mode (section 3.4.2), which can be set using the MODE B command. Although this feature is technically required for conformance to the specification, I'm not sure all FTP server software implements it.

在块传输模式下,与流传输模式相反,服务器以块的形式发送文件,每个块都有一个标记.可以将该标记重新提交给服务器,以重新启动失败的传输(第3.5节).

In the block transmission mode, as opposed to the stream transmission mode, the server sends the file in chunks, each of which has a marker. This marker may be re-submitted to the server to restart a failed transfer (section 3.5).

规范说:

[...]提供了重新启动过程,以保护用户免受严重的系统故障(包括主机,FTP进程或基础网络的故障).

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

但是,对于AFAIK,该规范未定义标记所需的寿命.它只说以下内容:

However, AFAIK, the specification does not define a required lifetime for markers. It only says the following:

标记信息仅对发送者有意义,但必须由控制连接的默认或协商语言(ASCII或EBCDIC)中的可打印字符组成.标记可以表示位数,记录数或系统可以用来标识数据检查点的任何其他信息.数据接收者如果执行重新启动过程,则会在接收系统中标记该标记的相应位置,并将此信息返回给用户.

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会话之间有效的标记,但是您的里程可能会有所不同.

It should be safe to assume that servers implementing this feature will provide markers that are valid between FTP sessions, but your mileage may vary.

这篇关于超时后恢复FTP下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆