python类与对象-如何让对象支持上下文管理

如何让对象支持上下文管理

问题举例html

一个telnet客户端的类TelnetClient, 调用实例的connect(),login(),interact方法python

启动客户端与服务器交互,交互完毕后须要调用cleanup()方法,关闭已链接的socket,编程

以及将操做历史记录写入文件并关闭。服务器

 

可否让TelnetClient的实例支持上下文管理协议,从而代替手工调用connect(),cleanup()方法。app

 

解决思路

实现上下文管理协议,即实现类的__enter__, __exit__方法,socket

它们分别在with开始和结束时被调用。url

 

代码spa

说明:这段代码并不能直接运行不了,由于服务器ip链接不上,.net

这里你们感觉下大体实现思路便可便可code

 

from sys import stdin, stdout import getpass import telnetlib from collections import deque class TelnetClient: def __init__(self, host, port=23): self.host = host self.port = port def __enter__(self): self.tn = telnetlib.Telnet(self.host, self.port) self.history = deque([]) return self def __exit__(self, exc_type, exc_value, exc_tb): print('IN __exit__', exc_type, exc_value, exc_tb) self.tn.close() self.tn = None with open('history.txt', 'a') as f: f.writelines(self.history) return True def login(self): # user
        self.tn.read_until(b"login: ") user = input("Enter your remote account: ") self.tn.write(user.encode('utf8') + b"\n") # password
        self.tn.read_until(b"Password: ") password = getpass.getpass() self.tn.write(password.encode('utf8') + b"\n") out = self.tn.read_until(b'$ ') stdout.write(out.decode('utf8')) def interact(self): while True: cmd = stdin.readline() if not cmd: break self.history.append(cmd) self.tn.write(cmd.encode('utf8')) out = self.tn.read_until(b'$ ').decode('utf8') stdout.write(out[len(cmd)+1:]) stdout.flush() # client = TelnetClient('192.168.0.105') # client.connect() # client.login() # client.interact() # client.cleanup()
 with TelnetClient('192.168.0.105') as client: raise Exception('TEST') client.login() client.interact() print('END')

__exit__中返回True是为了压制异常向上抛

 

参考资料:python3实用编程技巧进阶

相关文章
相关标签/搜索