ftps.storlines socket.timeout despite file upload completing

旧城冷巷雨未停 提交于 2019-12-06 12:54:14

问题


I'm trying to upload a CSV file using ftplib.FTP_TLS, however regardless of the timeout duration I set (5,10,60 seconds), the code always times out with the error:

File "/usr/lib/python3.4/ftplib.py", line 544, in storlines
  conn.unwrap()
File "/usr/lib/python3.4/ssl.py", line 788, in unwrap
  s = self._sslobj.shutdown()
socket.timeout: The read operation timed out

However after the timeout, I check the directory through Cyberduck, and the CSV file is there, complete.

Here is my upload code:

def upload_csv(filename):
    with FTP_TLS(timeout=5) as ftps:
        ftps.set_pasv(True)
        ftps.connect(ftps_server,ftps_port)
        ftps.login(ftps_username, ftps_password)
        ftps.prot_p()
        ftps.cwd('sales')
        ftps.storlines("STOR " + filename, open(filename,'rb'))

I've also tried storbinary(...) but get the same error.

Edit: The file definitely exists, and does actually get created on the server in its entirety. It looks like it's a problem with .shutdown() in ssl.py maybe waiting for a final read, but the internet doesn't seem to yield a solution.

Can anyone shed any light please?


回答1:


I was able to overcome the problem by overwriting

ftplib._SSLSocket = None

Here a more complete example:

def transfer_zip(dest_zipfile, host, host_path, user, password):
    os.chdir(dirname(dest_zipfile))
    with ftplib.FTP_TLS(host, timeout=10) as ftp:
        ftp.login(user, password)
        ftp.prot_p()
        ftp.cwd(host_path)
        ftplib._SSLSocket = None
        ftp.storbinary(f"STOR {basename(dest_zipfile)}", open(dest_zipfile, 'rb'))
        ftp.quit()
        ftp.close()


来源:https://stackoverflow.com/questions/28587229/ftps-storlines-socket-timeout-despite-file-upload-completing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!