mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
Fix issue #4972: adds ftplib.FTP context manager protocol
This commit is contained in:
parent
f95a1b3c53
commit
bd576b75b7
5 changed files with 99 additions and 11 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue