如何避免python类中的重复代码

如何避免python类中的重复代码,python,python-2.7,Python,Python 2.7,在python类上做了很多工作之后,现在我的代码可以正常工作了 但我在脚本的每个地方都使用了以下代码: buff = '' while not buff.endswith('#'): resp = self.remote_conn.recv(9999) buff += resp print resp 任何人都可以帮我避免在下面的代码中多次使用上面的代码 class Asr: def

在python类上做了很多工作之后,现在我的代码可以正常工作了

但我在脚本的每个地方都使用了以下代码:

        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
任何人都可以帮我避免在下面的代码中多次使用上面的代码

class Asr:
    def __init__(self):

        ''' SSH connection Establish '''
        self.hostname = hostname
        self.net_username = net_username
        self.net_password = net_password
    def remote(self):
        self.remote_conn_pre = paramiko.SSHClient()
        self.remote_conn_pre.set_missing_host_key_policy(
             paramiko.AutoAddPolicy())
        self.remote_conn_pre.connect(self.hostname, username=self.net_username, password=self.net_password, look_for_keys=False, allow_agent=False)
        self.remote_conn = self.remote_conn_pre.invoke_shell()
    def disable_paging(self):
        self.remote_conn.send("terminal length 0\n")
    def inventory(self):

        self.remote()
        self.remote_conn.send("\n")
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.close()
    def version(self):
        self.remote()
        self.remote_conn.send("\n")
        self.remote_conn.send("ping 1.1.1.1\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.disable_paging()
        self.remote_conn.send("show inventory\n")
        buff = ''
        while not buff.endswith('#'):
            resp = self.remote_conn.recv(9999)
            buff += resp
            print resp
        self.remote_conn.close()
asr = Asr()
asr.inventory()
asr.version()

使用与上述相同的函数 例如:

每次调用函数并传递buff参数。 例如:


嗯。。制作一个函数?我不知道出于某种原因,如果制作函数它不工作。我是说帕拉米科正在断开连接。让我在帕拉米科层面尝试一下正在发生的事情,以获得更多的鼓励和理论基础。
def check_buff(self, buff):

    while not buff.endswith('#'):
        resp = self.remote_conn.recv(9999)
        buff += resp
        print resp
buff = ''
self.check_buff(buff)