diff options
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) |