简体   繁体   English

从python捕获scp的输出

[英]Capturing output of scp from python

I have a django server, and I have to upload some data through scp. 我有一个django服务器,我必须通过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 (ie where I called the command 'runserver') I have this output shown: 在运行页面的控制台窗口上(即我称为命令“ runserver”的位置),显示以下输出:

[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? 我在此答案中尝试了解决方案如何从Python函数调用中捕获标准输出? but it didn't work. 但这没用。 I tried Popen and subprocess and had no results as well. 我尝试了Popen和子过程,也没有任何结果。 Maybe I'm doing something wrong? 也许我做错了什么?

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. 这不会直接回答您的问题,但是我不会在python中执行raw scp命令,因为即使您有输出,也很难对其进行解析。 You should consider using tools like fabric to handle this. 您应该考虑使用诸如fabric工具来处理此问题。 It's pythonic and you have full control over the input/output. 这是pythonic,您可以完全控制输入/输出。 The operation same as scp is put . putscp相同的操作。 For an example you could check this SO answer . 例如,您可以检查此SO答案

Almost all command line operations can be done using fabric , you won't regret learning it. 几乎所有的命令行操作都可以使用fabric完成,您不会后悔学习它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM