Implicit FTPS to ShareFile fails with "operation timed out" in Python

1.4k Views Asked by At

When using Python to make a connection to ShareFile via implicit FTPS I get the following:

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

My tyFTP (required because implicit FTPS is not directly supported in ftplib) class comes from here: Python FTP implicit TLS connection issue. Here's the code:

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

This question has been asked previously, but the only solution provided is to hack the python code. Is that the best/only option?

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

There is also an old discussion about this issue on python.org: http://bugs.python.org/issue8108. The suggestion there is that this is an ambiguous situation that's difficult to fix (and maybe never was?)

Please note: I would have added comments to the existing questions, but my reputation was not high enough to comment (new stack exchange user).

2

There are 2 best solutions below

0
On

sometimes the help you need is your own.

In order to fix this without directly modifying ftplib code (which requires jumping through hoops on a Mac because you cannot easily write/modify files in your /System/Library) I overrode the storbinary method in ftplib.FTP_TLS. That's essentially using this fix for supporting implicit FTPS:

Python FTP implicit TLS connection issue

and then adding these lines to the class tyFTP, and commenting out the conn.unwrap() call, and replacing it with '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()
1
On

My issue with implicit ftp over TLS has bothered me for six months. This week, I decided it was time to fix it. Finally, I combined the code from George Leslie-Waksman and gaFF at here, and Manager_of_it here, and it work like champ! Thank you, those three persons.