1

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?

Community
  • 1
  • 1
francisco sollima
  • 7,952
  • 4
  • 22
  • 38
  • You should create a model, e.g. `Document`. with a `FileField` Attribute. But no idea how to create a model instance when `scp`'ing. The instance then notifies the user in a `view` and `template` with infos about correctness and copy time. – Timo Jul 10 '18 at 14:29

1 Answers1

1

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.

Community
  • 1
  • 1
Shang Wang
  • 24,909
  • 20
  • 73
  • 94