mirror of
https://github.com/python/cpython.git
synced 2025-10-09 08:31:26 +00:00
#25485: Add context manager support to Telnet class.
Patch by Stéphane Wirtel.
This commit is contained in:
parent
37f5421954
commit
4f09806e66
5 changed files with 35 additions and 5 deletions
|
@ -637,6 +637,12 @@ class Telnet:
|
|||
raise EOFError
|
||||
return (-1, None, text)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.close()
|
||||
|
||||
|
||||
def test():
|
||||
"""Test program for telnetlib.
|
||||
|
@ -660,11 +666,10 @@ def test():
|
|||
port = int(portstr)
|
||||
except ValueError:
|
||||
port = socket.getservbyname(portstr, 'tcp')
|
||||
tn = Telnet()
|
||||
tn.set_debuglevel(debuglevel)
|
||||
tn.open(host, port, timeout=0.5)
|
||||
tn.interact()
|
||||
tn.close()
|
||||
with Telnet() as tn:
|
||||
tn.set_debuglevel(debuglevel)
|
||||
tn.open(host, port, timeout=0.5)
|
||||
tn.interact()
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue