gpt4 book ai didi

python - 超时后恢复 FTP 下载

转载 作者:太空狗 更新时间:2023-10-30 00:08:21 24 4
gpt4 key购买 nike

我正在从不稳定的 FTP 服务器下载文件,该服务器在文件传输过程中经常超时,我想知道是否有办法重新连接并恢复下载。我正在使用 Python 的 ftplib。这是我正在使用的代码:

#! /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] Connection timed out

非常感谢任何帮助。

最佳答案

仅使用标准设施(参见 RFC959)通过 FTP 恢复下载需要使用 block 传输模式(第 3.4.2 节),可以使用 MODE B 命令进行设置。尽管此功能在技术上是符合规范所必需的,但我不确定所有 FTP 服务器软件都实现了它。

在 block 传输模式中,与流传输模式相反,服务器以 block 的形式发送文件,每个 block 都有一个标记。此标记可能会重新提交给服务器以重新启动失败的传输(第 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 session 之间有效的标记应该是安全的,但您的里程可能会有所不同。

关于python - 超时后恢复 FTP 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6932280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com