class NotConnectedError(Exception):
'''没有建立连接啊,大guo'''
class ConnectedError(Exception):
'''还在连接中哦,大guo'''
class Connection:
def __init__(self):
self.new_state(CloseConnection)
def new_state(self,new_state):
self.__class__=new_state
def spawn(self):
'''
expect
:return:
'''
raise NotConnectedError()
def put(self):
'''
上传文件
:return:
'''
raise NotConnectedError()
def get_shell(self):
'''
获取shell
:return:
'''
raise NotConnectedError()
def open(self):
'''
建立连接
:return:
'''
raise NotConnectedError()
def close(self):
'''
关闭连接
:return:
'''
raise NotConnectedError()
class CloseConnection(Connection):
def open(self):
self.new_state(OpenConnection)
def close(self):
print('close')
class OpenConnection(Connection):
def spawn(self):
print('read')
def put(self):
print('write')
def get_shell(self):
print('get_shell')
def open(self):
return ConnectedError()
def close(self):
self.new_state(CloseConnection)
class NotConnectedError(Exception):
'''没有建立连接啊,大guo'''
class ConnectedError(Exception):
'''还在连接中哦,大guo'''
class Connection:
def __init__(self):
self.new_state(CloseConnection)
def new_state(self,new_state):
self.__class__=new_state
def spawn(self):
'''
expect
:return:
'''
raise NotConnectedError()
def put(self):
'''
上传文件
:return:
'''
raise NotConnectedError()
def get_shell(self):
'''
获取shell
:return:
'''
raise NotConnectedError()
def open(self):
'''
建立连接
:return:
'''
raise NotConnectedError()
def close(self):
'''
关闭连接
:return:
'''
raise NotConnectedError()
class CloseConnection(Connection):
def open(self):
self.new_state(OpenConnection)
def close(self):
print('close')
class OpenConnection(Connection):
def spawn(self):
print('read')
def put(self):
print('write')
def get_shell(self):
print('get_shell')
def open(self):
return ConnectedError()
def close(self):
self.new_state(CloseConnection)