3

我想知道是否有人观察到,与通过 Windows 命令提示符或使用 Perl 的Net::FTP模块执行 FTP get/put 相比,使用 Python 的ftplib通过 ftp 下载或上传文件所花费的时间非常大。

我创建了一个类似于http://code.activestate.com/recipes/521925-python-ftp-client/的简单 FTP 客户端,但我无法达到在 Windows DOS 提示符下运行 FTP 或使用 perl 时获得的速度. 是我遗漏了什么,还是 Python ftplib 模块有问题。

如果您能解释一下为什么我的 Python 吞吐量很低,我将不胜感激。

4

2 回答 2

5

问题出在块大小上,我使用的块大小为 1024,太小了。将块大小增加到 250Kb 后,所有不同平台的速度都相似。

def putfile(file=file, site=site, dir=dir, user=())
    upFile = open(file, 'rb')
    handle = ftplib.FTP(site)
    apply(handle.login, user)
    print "Upload started"
    handle.storbinary('STOR ' + file, upFile, 262144)
    print "Upload completed"
    handle.quit()
    upFile.close()
于 2013-06-04T21:04:32.320 回答
1

我在使用FTP_TLS的默认块大小 8192 时遇到了类似的问题

site = 'ftp.siteurl.com'
user = 'username-here'
upass = 'supersecretpassword'
ftp = FTP_TLS(host=site, user=user, passwd=upass)
with open(newfilename, 'wb') as f:
    def callback(data):
        f.write(data)
    ftp.retrbinary('RETR filename.txt', callback, blocksize=262144)

增加块大小会使速度提高 10 倍。谢谢@Tanmoy Dube

于 2017-06-12T20:19:33.467 回答