2

I want to be able to copy a file from a remote machine using either scp, ssh or sftp in python. The best way I've found is to use ssh with sftp. I've been trying to use this example to accomplish what I need to do.

import paramiko
import os

paramiko.util.log_to_file('logfile.log')

host = "101.102.103.104"
port = 22
transport = paramiko.Transport((host, port))
password = "pass"
username = "user"

sftp = paramiko.SFTPClient.from_transport(transport)

filepath = '~/remote/file'
localpath = '~/local/file'
sftp.get(filepath, localpath)

sftp.close()
transport.close()

When I do this, I receive this error:

Traceback (most recent call last):
  File "example.py", line 19, in <module>
    sftp.get(filepath, localpath)
AttributeError: 'NoneType' object has no attribute 'get'

Is there something I'm missing or not doing correctly?

Thank you

2
  • this call sftp = paramiko.SFTPClient.from_transport(transport) is returning None. Figure out why thats happening Commented Jun 22, 2012 at 14:11
  • well obviously your sftp object was not created Commented Jun 22, 2012 at 14:11

2 Answers 2

1

Your username and password are initialized but never used. You need to call connect for your transport object

password = "pass"
username = "user"
transport.connect(username = username, password = password)
Sign up to request clarification or add additional context in comments.

3 Comments

You're absolutely right, I completely missed that. Thanks for pointing it out. But now when I run it, I get an IOError saying the file doesn't exist in sftp.get(filepath, localpath) but they definitely do exist
make sure all directories in the localpath exist before calling sftp.get()
I figured it out, it's because I have to provide the full path as oppose to using "~/"
0

The API specifies you have to provide a "socket like object" to paramiko.Transport() you provide a tuple. I guess that's the problem.

UPDATE: Although specified otherwise in the docs the tuple is ok.

2 Comments

The documentation says that, but the source code (particularly, line 281 and just after) shows that Transport does accept a tuple.
Yes, i have just found out, too. Well as i said only a "guess" don't have paramiko available for testing right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.