[Tutor] paramiko again

Alan Gauld alan.gauld at btinternet.com
Sun May 10 01:53:49 CEST 2009


"Matt Herzog" <msh at blisses.org> wrote

> This code comes straight from the 
> http://oreilly.com/catalog/9780596515829/ book.
>
> The only actual code I changed was s/get/put on the second to last line.
> The author says I ought to be able to do this and have it Just Work.

> There are several things I don't understand.

What are they? You raise a few issues below but are they all?

> The code runs but does not upload any files.

How do you know? Where are you looking for them?
What are you looking for?

> I would rather be specifying the local dir on the source machine
> rather than path on the destination.

Have you used normal ftp in its command line version?
The put command specifies the location on the  remote machine
where you want to store the files. This is normal ftp behaviour.

> 1. How can paramiko know whether the dir_path is on the
> local vs remote system?

It always expects it to be on the remote machine I guess.

> 2. I can't find sftp.put or sftp.get in ipython.
> Are they part of paramiko?

Yes. sftp is an object that you create with a call from paramiko.
The class exists in paramiko and you create an instance of it
called sftp.

        sftp = paramiko.SFTPClient.from_transport(t)

> If not, where do they come from?

So get./put will be methods of whatever class
SFTPClient.from_transport() returns an instance of.

> ------------------- begin --------------------------
> #!/usr/bin/env python
> import paramiko
> import os
> hostname = '192.168.1.15'
> port = 22
> username = 'revjimjones'
> password = 'C0ol4id3'
> dir_path = '/home/revjimjones/logs'
> if __name__ == "__main__":
>    t = paramiko.Transport((hostname, port))
>    t.connect(username=username, password=password)
>    sftp = paramiko.SFTPClient.from_transport(t)
>    files = sftp.listdir(dir_path)

This gets a list of the files from dir_path on the remote machine.

>    for f in files:
>        print 'Uploading', f
>        sftp.put(os.path.join(dir_path, f), f)

So you are trying to upload files that currently exist on the
remote machine from your machine, that is unlikely to work.
It would only work if you happen to have files of exactly the
same name (updated versions maybe?). In that case you
will overwrite the remote server versions with your local copy.

I suspect you probably need to change the listdir() call to use
os.listdir(local_path) instead of stftp.listdir(). Where local_path
is wherever you store the files on the local mnachine that you
want to transfer.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list