If you create a modified session, for example, to specify a port to connect... You create a connection with this session and you try to do a list_dir, for example, if this operation fails, you catch the exception but the session object increments its reference counting and cannot be deleted by the garbage collector... Every time you do this operation in a loop and the exception is raised, the reference grows...
The code should be something like:
class MySession:
def __init__(self, host, port, user, passwd):
ftplib.FTP.__init__(self)
self.connect(host, port)
self.login(user, passwd)
self.conn = ftputil.FTPHost(m,
source_ftp_port,
source_ftp_user,
source_ftp_pass)
session_factory=MySession)
try:
self.conn.listdir('adir')
except:
pass
del self.conn