New submission from Jürgen <jot...@sags-per-mail.de>:

Hi,

I'm not quite sure, if you would actually call this a bug, but it is very 
molesting at least ;o)

I use ftplib.FTP_TLS to connect to a z/OS ftp server. With a minor change it 
works very well (happy to have found this library).
The problem I have is, that without any change, an exception is raised after 
every single command I invoke, even though the server sends back an ok message.

The exception is an OSError which is raised while executing conn.unwrap(). It 
seems the connection is already closed when this is called and thus an 
exception is raised. But handling this exception outside the FTP_TLS-class 
makes no sense, because then every command would raise an exception and the 
"good" exceptions could not be distinguised from the ones that are really 
searious so easily anymore (I mean: if i get an exception that a connection 
could not be closed, because someone else closed it before, that's not very 
serious, is it?).

Suggestions to solve this:
small solution: allow the programmer to decide what to do, by creating 
subclasses
This is "factor-out" the unwrap logic in a separate method or function, so at 
least users of the class can overwrite the behavior, without having to rebuild 
the whole logic of the affected methods.

In my quick solution I created a new method in class FTP:
    def __handleAutoCloseSSL__(self, conn):
        if self.autoCloseModeSSL == 'NONE' or self.autoCloseModeSSL is None or 
_SSLSocket is None or not isinstance(conn, _SSLSocket):
            # do nothing
            pass
        elif self.autoCloseModeSSL in ('SAFE', 'HIDE'):
            try:
                conn.unwrap()
            except OSError as ex:
                if self.autoCloseModeSSL != 'HIDE':
                    print('Caught exception %s while calling conn.unwrap()' % 
str(ex))
        else:
            # Standard mode (usally self.autoCloseModeSSL =='STANDARD' but 
anything else is accepted as well)
            # the original code was:
            #if _SSLSocket is not None and isinstance(conn, _SSLSocket):
            #    conn.unwrap()
            conn.unwrap()

And the class variable:
autoCloseModeSSL = 'STANDARD'

Then I called it from methods (instead of doing conn.unwrap() there directly):
retbinary
retlines
storbinary
storlines

Ok, maybe not that sexy, but it works :o)
And if you don't like the hack with instance variable autoCloseModeSSL, you 
could just transfer the original conn.unwrap() in an extra method which could 
then be overwritten by programmers in subclasses. This would already help me 
very much, because I know that patching a library is not a good idea. Even more 
if it is a communication library that might be updated from time to time.

----------
components: Library (Lib)
messages: 314261
nosy: jottbe
priority: normal
severity: normal
status: open
title: ftplib: FTP_TLS seems to have problems with sites that close the 
encrypted channel themselfes
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33122>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to