下面的程序给出以下输出:
Listing directory ftp://ftp.ncbi.nih.gov/pub/geo/DATA/SeriesMatrix
1.1 Mbyte
229.0 s
38 Kbit/s
Downloading file ftp://ftp.ncbi.nih.gov/pub/geo/DATA/SeriesMatrix/GSE30/GSE30_series_matrix.txt.gz
2.3 Mbyte
3.1 s
6221 Kbit/s
为什么目录列表与文件下载相比如此缓慢?有什么方法可以加快目录列表的速度吗?
这是代码
from __future__ import division
import ftplib
import time
server = 'ftp.ncbi.nih.gov'
dirPath = '/pub/geo/DATA/SeriesMatrix'
filePath = '/pub/geo/DATA/SeriesMatrix/GSE30/GSE30_series_matrix.txt.gz'
ftp = ftplib.FTP(server)
ftp.login('anonymous', 'john.smith@gmail.com')
print 'Listing directory ftp://' + server + dirPath
fileNames = []
t = time.clock()
ftp.retrlines('NLST ' + dirPath, fileNames.append)
t = time.clock() - t
size = sum([len(fileName) + 1 for fileName in fileNames])
print '{0:.1f} Mbyte\n{1:.1f} s\n{2:.0f} Kbit/s'.format(size / 2**20, t, 8 * size / 2**10 / t)
print '\nDownloading file ftp://' + server + filePath
blocks = []
t = time.clock()
ftp.retrbinary('RETR ' + filePath, blocks.append)
t = time.clock() - t
size = sum([len(block) for block in blocks])
print '{0:.1f} Mbyte\n{1:.1f} s\n{2:.0f} Kbit/s'.format(size / 2**20, t, 8 * size / 2**10 / t)
print
raw_input("Press Return to continue")