user1186173
user1186173

Reputation: 565

Copying files from Remote machine using sftp in python

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

Upvotes: 2

Views: 11196

Answers (2)

snies
snies

Reputation: 3521

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.

Upvotes: 0

Sergey Golovchenko
Sergey Golovchenko

Reputation: 18681

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)

Upvotes: 1

Related Questions