尽管文件上传完成,ftps.storlines socket.timeout

编程入门 行业动态 更新时间:2024-10-25 12:18:27
本文介绍了尽管文件上传完成,ftps.storlines socket.timeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使用 ftplib.FTP_TLS 上传 CSV 文件,但是无论我设置的超时持续时间(5、10、60 秒)如何,代码总是超时并出现错误:

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

但是超时后,我通过Cyber​​duck查看目录,CSV文件在那里,完成.

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

这是我的上传代码:

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'))

我也试过 storbinary(...) 但得到同样的错误.

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

该文件肯定存在,并且确实是在服务器上完整创建的.ssl.py 中的 .shutdown() 似乎有问题,可能正在等待最终读取,但互联网似乎没有产生解决方案.

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.

有人可以帮忙吗?

推荐答案

我能够通过覆盖来克服这个问题

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()

这篇关于尽管文件上传完成,ftps.storlines socket.timeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 11:51:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件上传   ftps   storlines   socket   timeout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!