francisco sollima
francisco sollima

Reputation: 8338

Capturing output of scp from python

I have a django server, and I have to upload some data through scp. I have this view:

pathFile = '/home/user1/foo.json'
user = 'user1scp'
server = 'someserver.com'
pathServer = '/var/www/foo.json'

os.system("scp %s %s@%s:%s" % (pathFile, user, server, pathServer))

On the console window where the page is running (i.e. where I called the command 'runserver') I have this output shown:

[21/Jul/2016 18:55:12] "GET /someurl/upload HTTP/1.1" 301 0
foo.json                                          100%  609     0.6KB/s   00:00 

I want to be able to manipulate that output, so I can notify the user that all the files (there are multiple files to upload) were upload correctly, or which were not.

I tried the solution in this answer How to capture stdout output from a Python function call? but it didn't work. I tried Popen and subprocess and had no results as well. Maybe I'm doing something wrong?

Upvotes: 1

Views: 895

Answers (1)

Shang Wang
Shang Wang

Reputation: 25549

This doesn't directly answer your question, but I won't do the raw scp command in python because the output is hard to parse even if you have it. You should consider using tools like fabric to handle this. It's pythonic and you have full control over the input/output. The operation same as scp is put. For an example you could check this SO answer.

Almost all command line operations can be done using fabric, you won't regret learning it.

Upvotes: 1

Related Questions