vieroli
vieroli

Reputation: 376

Keep ssh connection active

I have made a library who looks like this :

class AXISSetGet(object):

    def SSH_Start(self):
        cfg = yaml.load(open(config_file_path))
        HOST =  cfg['afo_axis_host']
        PORT = cfg['afo_axis_port']
        USERNAME = cfg['afo_axis_username']
        PASSWORD = cfg['afo_axis_password']

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=HOST, port=PORT, username=USERNAME, password=PASSWORD)

    def Get_File (self,filename):
    # rest of the function

SSH_Start() function is needed in every others functions of the class. I tried to write the SSH_Start() before declaring the class but it doesn't work..

Can anyone help me on this ? Thanks !

Upvotes: 0

Views: 78

Answers (2)

Holloway
Holloway

Reputation: 7367

Add the SSH client as an instance attribute that can then be used from any method. From your existing code, I don't think you want to call the SSH_Start method more than once, you just need the SSH connection to be open. Try something like:

class AXISSetGet(object):
    def __init__(self):
        self.SSH_Start() # Call it here to ensure the connection has been made before it's needed.

    def SSH_Start(self):
        # config stuff

        ssh = paramiko.SSHClient()
        # ...
        self.ssh = ssh

    def Get_File (self,filename):
        self.ssh.use_client_here(...)

I would be tempted to look at properties here. That way, the first time you try and use the client, it will create one and connect, then it will use the existing one for subsequent calls. Something like:

class AXISSetGet(object):
    def __init__(self):
        self._ssh = None

    @property
    def ssh(self):
        # if this is the first time the connection has been needed, create it
        if not self._ssh:
            # config stuff
            ssh = paramiko.SSHClient()
            # ...
            self._ssh = ssh
        # and then just return the existing one
        return self._ssh

    def Get_File (self,filename):
        self.ssh.use_client_here(...)

Just as a side note, have a read through of PEP8 for guidelines about naming conventions and formatting. It doesn't change how the code behaves but make it easier for others to read and understand.

Upvotes: 1

Sammy J
Sammy J

Reputation: 1066

You can use self to access functions of the same class like this, I guess this is what you want?

class AXISSetGet(object):

    def SSH_Start(self):
        cfg = yaml.load(open(config_file_path))
        HOST =  cfg['afo_axis_host']
        PORT = cfg['afo_axis_port']
        USERNAME = cfg['afo_axis_username']
        PASSWORD = cfg['afo_axis_password']

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=HOST, port=PORT, username=USERNAME,password=PASSWORD)

    def Get_File (self,filename):
        self.SSH_Start()

Upvotes: 1

Related Questions