[ftputil] Setting a passive/active connection during the same connection

Stefan Schwarzer sschwarzer at sschwarzer.net
Sun Mar 23 18:36:39 CET 2008


Hello Marco,

On 2008-03-23 17:48, Marco Buccini wrote:
> Is there a way to set a modality passive/active during the connection 
> and not only before the connection?
> 
> Infact with ftplib we have:
> 
> ftp = ftplib.FTP(host,user,pass, port) #or some similar
> ftp.set_pasv(True|False)
> 
> Instead in ftputil we have to define a class as in the example in the 
> documentation.

Principally, the instantiated session object is accessible
as FTPHost._session. Accessing it, for example via
host._session.set_pasv(True) is not recommended: ftputil
makes new sessions internally for child session objects
which are used for file-like objects.

So the recommended way is still to use a session factory
callable - unless you are hundred percent sure you avoid
use FTP file-like objects and want to risk compatibility
problems with future ftputil versions. ;-)

Note that the session_factory can by any callable, not
necessarily a class. Thus, you could use

def factory(host, user, pass, port):
    session = ftplib.FTP(host, user, pass, port)
    session.set_pasv(True)
    return session

host = ftputil.FTPHost(host, user, pass, port,
                       session_factory=factory)

which is a bit shorter than using a class as the session
factory.

Best regards,
Stefan


More information about the ftputil mailing list