美文网首页
python sarge 总结

python sarge 总结

作者: 讷讷01 | 来源:发表于2017-10-14 22:57 被阅读0次

Sarge

基本信息

基本使用

sarge.capture_stdout

捕获标准输出

In [81]: p = capture_stdout("ls -a / | tail -n  1")

In [82]: p.commands
Out[82]: [Command('ls -a /'), Command('tail -n 1')]

In [83]: p.returncode
Out[83]: 0

In [84]: p.returncodes
Out[84]: [0, 0]

In [85]: p.stdout
Out[85]: Capture-2

In [86]: p.stdout.threads
Out[86]: [<Thread(Thread-3355, stopped daemon 123145311330304)>]

In [87]: p.stdout.text
Out[87]: u'\u7528\u6237\u4fe1\u606f\n'

In [88]: print p.stdout.text
用户信息

sarge.capture_both 是 stderr\stdout 的合并

In [121]: from sarge import capture_stdout, run, capture_both

In [122]: a = capture_both("ls -a /ddd | tail -n  2")

In [123]: a.stderr
Out[123]: Capture-7

In [124]: a.stderr.text
Out[124]: u'ls: /ddd: No such file or directory\n'

In [125]: a.stdout.text
Out[125]: u''

In [126]: a.returncodes
Out[126]: [1, 0]

sarge.run

In [91]: k = run("ls -a / | tail -n  2")
virtualenvs
用户信息

In [93]: print k.stdout
None

In [94]: print k.returncodes
[0, 0]

# Capture 可以捕获多个命令的输出
>>> from sarge import run, Capture
>>> p = run('echo foo; echo bar; echo baz', stdout=Capture())
>>> p.stdout.readline()
'foo\n'
>>> p.stdout.readline()
'bar\n'
>>> p.stdout.readline()
'baz\n'
>>> p.stdout.readline()
''

# input 使用
In [81]: p = run('cat', input='foo')
foo

异步执行

>>> from sarge import run, Capture
>>> cmd = 'echo foo & (sleep 2; echo bar) & (sleep 10; echo baz)'
>>> p = run(cmd, stdout=Capture(), async=True) # returns immediately
>>> p.close() # wait for completion[等待命令全部执行完成]
>>> p.stdout.readline()
'foo\n'
>>> p.stdout.readline()
'baz\n'
>>> p.stdout.readline()
'bar\n'
>>>

sarge.get_stderr\sarge.get_stdout\sarge.get_both

相关文章

  • python sarge 总结

    Sarge 基本信息 文档:http://sarge.readthedocs.io/en/latest/overv...

  • python学习总结

    python基础语法总结 参考:Python基础语法总结 参考:1.Python标识符 在 Python 里,标识...

  • Python基础之元组、字典,集合详解

    之前总结了Python列表,这篇总结Python的元组,字典和集合。 一 元组 tuple Python 的元组与...

  • 2019-10-23

    python面向对象编程总结 python中的对象:在其...

  • python总结系列笔记的一点说明

    今天有两个网友,告知我python总结(仅仅python总结系列,不包含python数据分析等其它资料)系列课程侵...

  • python enumerate 输出序列号加内容

    python enumerate用法总结 enumerate()说明 enumerate()是python的内置函...

  • 2017.6.13-14

    学习python总结python常用的方法string的常用方法dictionary的常用方法 python抽象,...

  • python 命令

    python 常用命令总结 http://www.runoob.com/python3/python3-upper...

  • python format 函数总结

    python format 函数总结 文章基于Python2.7.12进行讲述: 未完待续。。。。。。

  • python学习总结

    关于Python的一些总结 希望自己以后在学习Python的过程中可以边学习边总结,就自己之前的学习先做以总结,之...

网友评论

      本文标题:python sarge 总结

      本文链接:https://www.haomeiwen.com/subject/tftquxtx.html