diff options
author | Giampaolo RodolĂ <g.rodola@gmail.com> | 2010-05-10 14:53:29 (GMT) |
---|---|---|
committer | Giampaolo RodolĂ <g.rodola@gmail.com> | 2010-05-10 14:53:29 (GMT) |
commit | bd576b75b7bed253b7bf4af5a967e3ee4dc1af8a (patch) | |
tree | fe5924f78b230670672d6097bc3be6724fed9b27 /Lib/ftplib.py | |
parent | f95a1b3c53bdd678b64aa608d4375660033460c3 (diff) | |
download | cpython-bd576b75b7bed253b7bf4af5a967e3ee4dc1af8a.zip cpython-bd576b75b7bed253b7bf4af5a967e3ee4dc1af8a.tar.gz cpython-bd576b75b7bed253b7bf4af5a967e3ee4dc1af8a.tar.bz2 |
Fix issue #4972: adds ftplib.FTP context manager protocol
Diffstat (limited to 'Lib/ftplib.py')
-rw-r--r-- | Lib/ftplib.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py index a0c3578..c25ae2a 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -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) |