How to send a file using scp using python 3.2?

I am trying to send a group of files to a remote server via no-ack python byndings for libssh2, but I completely lost the use of the library due to lack of documentation.

I tried using C documents for libssh2 unsuccessfully.

Since I'm using python 3.2, paramiko and pexpect are out of the question. Who can help?

EDIT: I just found the code in the no-Ack blog comments on my post.

import libssh2, socket, os SERVER = 'someserver' username = 'someuser' password = 'secret!' sourceFilePath = 'source/file/path' destinationFilePath = 'dest/file/path' sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((SERVER, 22)) session = libssh2.Session() session.startup(sock) session.userauth_password(username, password) sourceFile = open(sourceFilePath, 'rb') channel = session.scp_send(destinationFilePath, 0o644, os.stat(sourceFilePath).st_size) while True: data = sourceFile.read(4096) if not data: break channel.write(data) exitStatus = channel.exit_status() channel.close() 

It seems to be working fine.

+2
ssh libssh2
source share
2 answers

Here's how to get files with libssh2 in Python 3.2. It is very important for me. You will need Python3 bindings for libssh2 https://github.com/wallunit/ssh4py

 import libssh2, socket, os SERVER = 'someserver' username = 'someuser' password = 'secret!' sourceFilePath = 'source/file/path' destinationFilePath = 'dest/file/path' sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((SERVER, 22)) session = libssh2.Session() session.startup(sock) session.userauth_password(username, password) (channel, (st_size, _, _, _)) = session.scp_recv(sourceFilePath, True) destination = open(destinationFilePath, 'wb') got = 0 while got < st_size: data = channel.read(min(st_size - got, 1024)) got += len(data) destination.write(data) exitStatus = channel.get_exit_status() channel.close() 
+4
source share

Do this in Python (i.e. do not wrap scp through subprocess.Popen or the like) using the Paramiko library.

Revelent: http: //stackoverflow.com

0
source share

All Articles