Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFTP File Handler doesn't raise Exception if the SSH Client or SFTP session are out of scope #53

Open
victorhooi opened this issue Nov 15, 2011 · 0 comments

Comments

@victorhooi
Copy link

Hi,

I had some Python code with two functions, let's call them alice() and bob().

alice() would call bob(), and bob() would open up a SSH session, and return a SFTP file handler to alice().

#!/usr/bin/env python2
import paramiko

def get_instances_cfg():
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('some_server', username='victorhooi')
    sftp = client.open_sftp()
    file = sftp.file('/tmp/instances.cfg')

    # This would print out the file fine.
    # for row in file:
    #    print(row)

    return file
    #sftp.close()
    #client.close()

def get_root_directory():
    instances_cfg = get_instances_cfg()

    # The following would print out nothing.
    for row in instances_cfg:
        print(row)

if __name__ == '__main__':
    get_root_directory()

I could iterate over the file handler fine in bob(), however in alice() it seemed to silently fail, printing nothing.

Anyhow, somebody pointed out to me that it was probably a scoping issue - when I left bob(), client and sftp would go out of scope, even though I returned file.

I made client, sftp and sftp global, and this then worked fine - I could iterate over file in alice().

My question is - I assume this is by design, right? The SFTP file handler won't work if the client doesn't exist in your current scope?

Should there be some kind of error message or exception raised if you try to access a SFTP file handler that doesn't have the original SSHClient or SFTP session around?

In my case, it seemed to be a valid SFTP file handler, but it just didn't contain any rows.

Cheers,
Victor

intgr pushed a commit to intgr/paramiko that referenced this issue Nov 27, 2019
tests: update travis-ci python versions and flake8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant