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實用編程技巧進階

相關文章
相關標籤/搜索