ftplib - 文件创建非常慢:SSLError:读取操作超时 [英] ftplib - file creation very slow: SSLError: The read operation timed out

查看:685
本文介绍了ftplib - 文件创建非常慢:SSLError:读取操作超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import ftplib 
import csv
import StringIO

sio = StringIO.StringIO()
data = csv.writer (sio)
data.writerows(data_to_export)
sio.seek(0)
ftps.storbinary(STOR+'blah.csv',sio)
ftps.close ()

所以我创建的文件只有56行,创建速度非常慢。当它完成创建,我得到这个错误:

  ftps.storbinary(STOR+'blah.csv',sio )
文件/usr/lib/python2.7/ftplib.py,行752,在storbinary
conn.unwrap()
文件/usr/lib/python2.7/ ssl.py,第384行解包
s = self._sslobj.shutdown()
SSLError:读取操作超时

我不明白为什么它太慢了,为什么它会给出时间,即使它创建该文件。



< PS 请问是否需要提供额外的信息?

更新
我也试过 cString ,但它不会改善任何东西,因为我猜它与写入速度无关。

如果有任何区别,连接到ftp是使用隐式SSL / TLS模式(该ftp不支持显式SSL / TLS)。


$ b

更新
深入挖掘。这就是调试显示的ftp:

  * cmd *'TYPE I'
* resp *'200二进制模式选择'
* cmd *'PASV'
* resp *'227进入被动模式(*,*,*,*,*,*)'
* cmd *'STOR等等。 csv'
* resp *'125安全的数据连接打开;转移开始

然后在最后一次输出中查找,直到连接超时。



注意。我的互联网连接非常好,但是这个ftp有点慢。我仍然认为这样的小文件应该比这更快处理。



Update2
这是一些奇怪的ftp。尝试不安全的FTP连接,文件上传正确和快速。这种隐式连接与这种缓慢的性能和超时有关。

解决方案

在速度方面,猜测,所以我会调整代码,并尝试下面的代码,我最近创建的客户FTP传递,并可以表明它的工作原理没有错误。

注意这是从更大的文件,但已经提取了相关的部分,所以忽略了一些变量,即args.var,你可能需要调整块大小取决于带宽等尝试把它降低到1024然后进行。



希望它有帮助
$ b $ pre $ def $ initiate_ftp_connection(ftp_host,user,passwd,ftp_dir)
ftp_session = ftplib.FTP()
ftp_session.connect(ftp_host,21)
#uncomment进行调试。
#ftp_session.set_debuglevel(2)
ftp_session.login(user = user,
passwd = passwd)
#cd修正远程目录
ftp_session.cwd(ftp_dir )
return ftp_session


$ b def upload_deliverables(session,file_and_path):
working_dir = os.path.dirname(file_and_path)
#删除任何换行符
filename = os.path.basename(file_and_path).strip()
totalSize = os.path.getsize(file_and_path)
#instantiate状态更新的进度跟踪器
uploadTracker = FtpUploadTracker(int(totalSize),filename)
#将dir改为working_dir
os.chdir(working_dir)
'''
触发ftp上传(storbinary)交付的。
Args:
1:FTP KEYWORD和FILE
2:文件IO
3:块大小
4:回叫
'''
会话.storbinary('STOR'+ filename,
open(filename,'r'),
8192,$ b $ uploadTracker.ftp_callback)


#connect到服务器
ftp_session = initiate_ftp_connection(args.ftp_host,
args.ftp_user,
args.ftp_pass,
args.ftp_dir)

#start ftp delivery
upload_deliverables(ftp_session,args.asset)
#quit ftp session
ftp_session.quit()
#关闭任何文件句柄。
ftp_session.close()


import ftplib
import csv
import StringIO

sio = StringIO.StringIO()
data = csv.writer(sio)
data.writerows(data_to_export)
sio.seek(0)
ftps.storbinary("STOR " + 'blah.csv', sio)
ftps.close()

So the file I'm creating is only 56 lines, it creates very slowly. And when it finishes creating, I get this error:

    ftps.storbinary("STOR " + 'blah.csv', sio)
  File "/usr/lib/python2.7/ftplib.py", line 752, in storbinary
    conn.unwrap()
  File "/usr/lib/python2.7/ssl.py", line 384, in unwrap
    s = self._sslobj.shutdown()
SSLError: The read operation timed out

I don't get it why it is so slow and why it gives time out, even though it creates that file.

P.S. please ask if I need to provide additional information

Update I also tried cString, but it does not improve anything as I guess its not related with writing speed.

If there is any difference, connection to ftp is made using Implicit SSL/TLS mode (that ftp does not support Explicit SSL/TLS).

Update Digging deeper. That is what debugging shows for ftp:

*cmd* 'TYPE I'
*resp* '200 Binary mode selected.'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (*,*,*,*,*,*)'
*cmd* 'STOR blah.csv'
*resp* '125 Secure data connection open; transfer starting.'

And then stucks at that last output till I get connection time out.

Note. My internet connection is very good, but that ftp is kind of slow. Still I guess such small file should be handled much faster than this.

Update2 That is some weird ftp. Trying unsecure ftp connection, files are uploaded properly and fast. That Implicit connection has something to do with this slow performance and time outs.

解决方案

In regards to speed there are too many factors to guess at, so i would adjust the code and try the following code which i recently created for customer ftp delivery and can state it works without error.

Note this is from a much larger file but have extracted the relevant sections so ignore some of the variables ie args.var, you may have to adjust the blocksize dependant on bandwidth etc try taking it down to 1024 then working up.

Hope it helps

def initiate_ftp_connection(ftp_host, user, passwd, ftp_dir):
    ftp_session = ftplib.FTP()
    ftp_session.connect(ftp_host, 21)
    #uncomment for debugging.
    #ftp_session.set_debuglevel(2)
    ftp_session.login(user=user, 
                      passwd=passwd)
    #cd to correct remote directory
    ftp_session.cwd(ftp_dir)
    return ftp_session



def upload_deliverables(session, file_and_path):
    working_dir = os.path.dirname(file_and_path)
    #strip to remove any newlines
    filename = os.path.basename(file_and_path).strip()
    totalSize = os.path.getsize(file_and_path)
    #instantiate progress tracker for status updates
    uploadTracker = FtpUploadTracker(int(totalSize),filename)
    #change dir to working_dir
    os.chdir(working_dir)
    '''
     Trigger the ftp upload (storbinary) for the deliverable.
      Args:
       1: FTP KEYWORD and FILE
       2: File IO
       3: Blocksize
       4: Callback
    '''
    session.storbinary('STOR ' + filename, 
                        open(filename,'r'), 
                        8192, 
                        uploadTracker.ftp_callback)


#connect to server
ftp_session = initiate_ftp_connection(args.ftp_host, 
                                      args.ftp_user, 
                                      args.ftp_pass,
                                      args.ftp_dir)

#start ftp delivery
upload_deliverables(ftp_session, args.asset)
#quit the ftp session
ftp_session.quit()   
#close any file handles.
ftp_session.close()

这篇关于ftplib - 文件创建非常慢:SSLError:读取操作超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆