Fix issue #4972: adds ftplib.FTP context manager protocol

This commit is contained in:
Giampaolo Rodolà 2010-05-10 14:53:29 +00:00
parent f95a1b3c53
commit bd576b75b7
5 changed files with 99 additions and 11 deletions

View file

@ -120,6 +120,20 @@ class FTP:
if user:
self.login(user, passwd, acct)
def __enter__(self):
return self
# Context management protocol: try to quit() if active
def __exit__(self, *args):
if self.sock is not None:
try:
self.quit()
except (socket.error, EOFError):
pass
finally:
if self.sock is not None:
self.close()
def connect(self, host='', port=0, timeout=-999):
'''Connect to host. Arguments are:
- host: hostname to connect to (string, default previous host)