当使用 Python 通过隐式 FTPS 连接到 ShareFile 时,我得到以下信息:
Traceback (most recent call last):
ftps.storbinary("STOR /file, open(file, "rb"), 1024)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 769, in storbinary
conn.unwrap()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 791, in unwrap
s = self._sslobj.shutdown()
SSLError: ('The read operation timed out',)
我的 tyFTP(必需,因为 ftplib 不直接支持隐式 FTPS)类来自这里:Python FTP implicit TLS connection issue .这是代码:
ftps = tyFTP()
try:
ftps.connect(‘ftps.host.domain’, 990)
except:
traceback.print_exc()
traceback.print_stack()
ftps.login(‘uid', ‘pwd')
ftps.prot_p()
try:
ftps.storbinary("STOR /file", open(file, "rb"), 1024)
# i also tried non-binary, but that didn't work either
# ftps.storlines("STOR /file", open(file, "r"))
except:
traceback.print_exc()
traceback.print_stack()
这个问题之前已经被问过,但是提供的唯一解决方案是破解 python 代码。这是最好/唯一的选择吗?
ShareFile upload with Python 2.7.5 code timesout on FTPS STOR
ftplib - file creation very slow: SSLError: The read operation timed out
ftps.storlines socket.timeout despite file upload completing
python.org 上也有关于此问题的旧讨论:http://bugs.python.org/issue8108 .那里的建议是,这是一个难以修复的模棱两可的情况(也许从来没有?)
请注意:我会为现有问题添加评论,但我的声誉不够高,无法发表评论(新堆栈交换用户)。
请您参考如下方法:
有时您需要的帮助是您自己的。
为了在不直接修改 ftplib 代码的情况下解决这个问题(这需要在 Mac 上跳过箍,因为你不能轻易地在/System/Library 中写入/修改文件)我覆盖了 ftplib.FTP_TLS 中的 storbinary 方法。这实质上是使用此修复来支持隐式 FTPS:
Python FTP implicit TLS connection issue
然后将这些行添加到类 tyFTP 中,并注释掉 conn.unwrap() 调用,并将其替换为“pass”:
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
self.voidcmd('TYPE I')
conn = self.transfercmd(cmd, rest)
try:
while 1:
buf = fp.read(blocksize)
if not buf: break
conn.sendall(buf)
if callback: callback(buf)
if isinstance(conn, ssl.SSLSocket):
pass
# conn.unwrap()
finally:
conn.close()
return self.voidresp()
