通过python中的ftplib上传比手动操作要长得多

时间:2013-12-18 14:17:46

标签: python python-2.7 file-upload ftplib

SO,

出于某种原因,下面的一小段代码上传的时间比我通过FTP程序或通过网站本身上传的时间长得多。

文件达到600kb,通过ftp程序/直接通过网络最多需要1-3秒,但是通过ftplib需要15-30秒...... 慢了15次

我上传的文件是单行html / php文件(缩小版),是否有更快的上传方式...?

在我的实际代码中,我上传了25个以上文件,文件总大小最多为1mb,是否有.tmp重命名的批量上传方法(防止部分更新的页面加载) )哪种方式对我有用?

import ftplib

print "Uploading FULL.html"
## Login using the ftplib library and set the session as the variable ftp_session
ftp_session = ftplib.FTP('ftp.website.com','admin@website.com','Password1','',60)
## Open a file to upload
ftp_file = open('OUTPUT/DATA/FULL.html','r')
## Open a folder in the ftp server
ftp_session.cwd("/DATA/")
## Send/upload the file to the ftp server
ftp_session.storlines('STOR FULL.html.tmp', ftp_file)
## Close the ftp_file
ftp_file.close()
## Rename temp file
ftp_session.rename("FULL.html.tmp", "FULL.html")

print "Uploading index.php"
## Open a file to upload
ftp_file = open('OUTPUT/index.php','r')
## Send/upload the file to the ftp server
ftp_session.storlines('STOR index.php.tmp', ftp_file)
## Close the ftp_file
ftp_file.close()
## Rename temp file
ftp_session.rename("index.php.tmp", "index.php")

print "Uploading secondary.php"
## Open a file to upload
ftp_file = open('OUTPUT/secondary.php','r')
## Send/upload the file to the ftp server
ftp_session.storlines('STOR secondary.php.tmp', ftp_file)
## Close the ftp_file
ftp_file.close()
## Rename temp file
ftp_session.rename("secondary.php.tmp", "secondary.php")

## Quit out of the FTP session
ftp_session.quit()

示例上传输出....

Running...
Uploaded MAIN.html (284KB) in 9.677 seconds at 29.40 kbps
Uploaded GROUP1.html (12KB) in 0.660 seconds at 19.12 kbps
Uploaded GROUP10.html (9KB) in 1.137 seconds at 8.07 kbps
Uploaded GROUP11.html (14KB) in 1.014 seconds at 14.56 kbps
Uploaded GROUP12.html (17KB) in 0.913 seconds at 18.87 kbps
Uploaded GROUP13.html (12KB) in 0.675 seconds at 18.66 kbps
Uploaded GROUP14.html (13KB) in 0.738 seconds at 18.54 kbps
Uploaded GROUP15.html (16KB) in 0.971 seconds at 16.53 kbps
Uploaded GROUP16.html (14KB) in 1.755 seconds at 8.44 kbps
Uploaded GROUP17.html (13KB) in 1.504 seconds at 9.05 kbps
Uploaded GROUP18.html (14KB) in 0.725 seconds at 20.47 kbps
Uploaded GROUP19.html (14KB) in 1.691 seconds at 8.76 kbps
Uploaded GROUP2.html (10KB) in 0.541 seconds at 18.90 kbps
Uploaded GROUP20.html (13KB) in 0.799 seconds at 16.69 kbps
Uploaded GROUP21.html (14KB) in 0.713 seconds at 20.42 kbps
Uploaded GROUP22.html (14KB) in 0.776 seconds at 18.61 kbps
Uploaded GROUP23.html (14KB) in 0.592 seconds at 24.99 kbps
Uploaded GROUP24.html (13KB) in 0.734 seconds at 18.86 kbps
Uploaded GROUP25.html (20KB) in 0.845 seconds at 24.49 kbps
Uploaded GROUP26.html (18KB) in 0.637 seconds at 28.91 kbps
Uploaded GROUP27.html (11KB) in 0.781 seconds at 14.54 kbps
Uploaded GROUP28.html (16KB) in 0.798 seconds at 20.25 kbps
Uploaded GROUP29.html (12KB) in 0.542 seconds at 23.46 kbps
Uploaded GROUP3.html (16KB) in 0.585 seconds at 27.47 kbps
Uploaded GROUP4.html (14KB) in 0.622 seconds at 23.97 kbps
Uploaded GROUP5.html (14KB) in 0.716 seconds at 20.54 kbps
Uploaded GROUP6.html (18KB) in 1.109 seconds at 16.48 kbps
Uploaded GROUP7.html (11KB) in 0.654 seconds at 17.52 kbps
Uploaded GROUP8.html (9KB) in 0.560 seconds at 16.25 kbps
Uploaded GROUP9.html (12KB) in 0.582 seconds at 21.42 kbps
The whole routine took 72.468 seconds

2 个答案:

答案 0 :(得分:2)

有几个原因导致您的传输速度尽可能慢,而且应用程序的发送速度要快得多:

  1. 您一次发送一行,ftp应用程序将一次发送一个8192字节的块,
  2. 您正在以文本模式发送,应用程序可能以二进制压缩模式发送,
  3. 除非行结尾有问题,否则使用open(filename, "rb")然后tp_session.storbinary('STOR index.php.tmp', ftp_file)您还可以尝试发送命令'MODE COMPRESSED'并检查回复,然后您可以使用块大小来查找最佳的。

    我要设置的另一件事就是:

    import glob, os, ftplib
    from time import time
    
    sourcedir = '/where/your/files/are/locally'
    destdir = '/where/to/put/them'
    ##    filelist = ['file2.ext', 'file2.ext', 'etc']
    filelist = glob.glob(os.path.join(sourcedir, '*.php'))
    
    ftp_session = ftplib.FTP('ftp.website.com','admin@website.com','password123','',60)
    ftp_session.cwd(destdir)
    
    for fname in filelist:
        filename = fname.replace(sourcedir+'\\', "")
        ftp_file = open(fname,'rb')
        result = ftp_session.storbinary('STOR %s.tmp' % filename, ftp_file)
        ftp_file.close()
        if 199 < int(result[0:3]) < 300: # Positive Completion
            ftp_session.rename('%s.tmp' % filename, filename)
        else:
            print "Upload of %s resulted in code %s" % (filename, result)
            break
    

    您可能会压缩或tar.gz您的文件并发送,但您需要一个安全的套接字连接和适当的权限来单独解压缩。

答案 1 :(得分:0)

定义blocksize以及ftp连接的storbinary,因此你将获得比FTP Filezilla快1.5-3.0倍的连接:)

from ftplib import FTP

USER = "Your_user_id"
PASS = "Your_password"
PORT = 21
SERVER = 'ftp.billionuploads.com' #use FTP server name here

ftp = FTP()
ftp.connect(SERVER, PORT)
ftp.login(USER, PASS)

try:
    file = open(r'C:\Python27\1.jpg','rb')
    ftp.storbinary('STOR ' + '1.jpg', file,102400) #here we store file in 100kb blocksize
    ftp.quit()
    file.close()
    print "File transfered"
except:
    print "Error in File transfering"