Python ftplib:低下载&使用python ftplib时上传速度

时间:2013-05-29 22:11:03

标签: python ftp ftplib throughput

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

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

如果你能说明为什么我用Python获得低吞吐量,我真的很感激。

2 个答案:

答案 0 :(得分:4)

问题在于块大小,我使用的块大小为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()

答案 1 :(得分:1)

我使用FTP_TLS

在默认的blockize 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

相关问题