结构-是否有任何方法可以捕获运行标准输出? [英] Fabric - Is there any way to capture run stdout?
问题描述
我正在尝试执行以下操作:
I'm trying to do the following:
output = run("ls -l backups")
for line in output.split("/n"):
do_stuff(line)
将ls
的stdout
发送到output
的任何方式?
Any way of having the stdout
of ls
sent to output
?
更具体地说,我正在使用名为s3cmd
的CLI应用程序,该应用程序的功能与ls
类似,但具有远程Amazon S3存储桶.
To be more specific I'm using a CLI app called s3cmd
which does something similar to ls
, but with remote Amazon S3 buckets.
因此,不幸的是,替换ls
不会有帮助.
So a replacement for ls
won't help unfortunately.
推荐答案
正是您所要求的.从文档:
run将以单个(可能是多行)字符串的形式返回远程程序的标准输出结果.
run will return the result of the remote program’s stdout as a single (likely multiline) string.
run()
以及相关的命令,例如local()
和sudo()
,返回一个_AttributeString
对象,该对象只是stdout的包装,可以通过属性访问其他信息,例如失败/成功布尔值,stderr,命令运行,等等.结果对象还具有stdout
属性,该属性更加明确.
run()
, and related commands like local()
and sudo()
, return an _AttributeString
object that is just a wrapper around stdout with attribute access to additional information like failure/success booleans, stderr, the command run, etc. The result object also has a stdout
attribute, which is just more explicit.
要进行故障排除,请print type(output), output
确保响应是您期望的.检查output.failed
和output.stderr
.可能是该命令没有按预期执行操作,没有备份"目录,等等.
To troubleshoot, print type(output), output
to be sure the response is what you expect. Examine output.failed
and output.stderr
. It could be the command isn't doing what you expect, there is no "backups" directory, etc.
这篇关于结构-是否有任何方法可以捕获运行标准输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!