In case when FTP url is successfully connected via default FTPHandler in FTPHandler.connect_ftp then release of this stuff becomes total responsibility of user i.e. socket remains open, which leads to ResourceWarning in the garbage collector, in case if the user does nothing.
Something like this solves issue in user area:
class SafeFTPHandler(urllib.request.FTPHandler):
ftp_object = None
def __init__(self):
super().__init__()
def connect_ftp(self, user, passwd, host, port, dirs, timeout):
self.ftp_object = super().connect_ftp(user, passwd, host, port, dirs, timeout)
return self.ftp_object
def ftp_response(self, req, response):
self._close_ftp()
return response
def ftp_open(self, req):
try:
return super().ftp_open(req)
except:
self._close_ftp()
raise
def _close_ftp(self):
if self.ftp_object:
self.ftp_object.close()
and further usage in OpenerDirector...
In general case FTPHandler should be able to close immediately (in case if it raises as well) while re-using of the socket should be implemented only within CacheFTPHandler
|