【python2】commands模块getstatusoutput函数的小问题

[TOC]

问题

import commands as subprocess
status, _ = subprocess.getstatusoutput("ps -elf|grep fuckU")
# status:
# 255
  • 这里的status按照grep的定义其实应该返回1,也就是没有grep到匹配项,在shell中echo $? 结果为1
    但是python2getstatusoutput获取的并不是os.exitcode()而是os.wait()的返回值。
  • python3由于使用的是subprocess = modified(commands + subprocess),同样执行
    status, _ = subprocess.getstatusoutput("ps -elf|grep fuckU")

    得到的status为正确的1

我是怎么解决这个问题

兼顾Python2和Python3的使用习惯,使用google开源的subprocess32代替commands

什么是subprocess32

谷歌将Python32的subprocess移植到了python2版本中。据说这是线程安全的版本。
最新的Python32版本为3.5.3也就是将python3.5.3中的subprocess基本功能移植到了Python2中。但是subprocess32不包含原本commands中的函数。

Timeout support backported from Python 3.3 is included.
The run() API from Python 3.5 was backported in subprocess32 3.5.0.
Otherwise features are frozen at the 3.2 level.

这里我们使用subprocess32.run()方法去模拟getstatusoutput等便捷的函数(python3官方实现)

# 原本的timeout=None,因为Python2不支持*args,**kwargs外再有kwargs,暂时移出参数项,放至函数体内
# def check_output(*popenargs, timeout=None, **kwargs):
def check_output(*popenargs, **kwargs):
    r"""Run command with arguments and return its output.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    b‘crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n‘

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    b‘ls: non_existent_file: No such file or directory\n‘

    There is an additional optional argument, "input", allowing you to
    pass a string to the subprocess‘s stdin.  If you use this argument
    you may not also use the Popen constructor‘s "stdin" argument, as
    it too will be used internally.  Example:

    >>> check_output(["sed", "-e", "s/foo/bar/"],
    ...              input=b"when in the course of fooman events\n")
    b‘when in the course of barman events\n‘

    If universal_newlines=True is passed, the "input" argument must be a
    string and the return value will be a string rather than bytes.
    """
    timeout = None
    if ‘stdout‘ in kwargs:
        raise ValueError(‘stdout argument not allowed, it will be overridden.‘)

    if ‘input‘ in kwargs and kwargs[‘input‘] is None:
        # Explicitly passing input=None was previously equivalent to passing an
        # empty string. That is maintained here for backwards compatibility.
        kwargs[‘input‘] = ‘‘ if kwargs.get(‘universal_newlines‘, False) else b‘‘

    return subprocess.run(*popenargs, stdout=subprocess.PIPE, timeout=timeout, check=True,
                          **kwargs).stdout

def getstatusoutput(cmd):
    """Return (exitcode, output) of executing cmd in a shell.

    Execute the string ‘cmd‘ in a shell with ‘check_output‘ and
    return a 2-tuple (status, output). The locale encoding is used
    to decode the output and process newlines.

    A trailing newline is stripped from the output.
    The exit status for the command can be interpreted
    according to the rules for the function ‘wait‘. Example:

    >>> import subprocess
    >>> subprocess.getstatusoutput(‘ls /bin/ls‘)
    (0, ‘/bin/ls‘)
    >>> subprocess.getstatusoutput(‘cat /bin/junk‘)
    (1, ‘cat: /bin/junk: No such file or directory‘)
    >>> subprocess.getstatusoutput(‘/bin/junk‘)
    (127, ‘sh: /bin/junk: not found‘)
    >>> subprocess.getstatusoutput(‘/bin/kill $$‘)
    (-15, ‘‘)
    """
    try:
        data = check_output(cmd, shell=True, universal_newlines=True, stderr=subprocess.STDOUT)
        exitcode = 0
    except subprocess.CalledProcessError as ex:
        data = ex.output
        exitcode = ex.returncode
    if data[-1:] == ‘\n‘:
        data = data[:-1]
    return exitcode, data

def getoutput(cmd):
    """Return output (stdout or stderr) of executing cmd in a shell.

    Like getstatusoutput(), except the exit status is ignored and the return
    value is a string containing the command‘s output.  Example:

    >>> import subprocess
    >>> subprocess.getoutput(‘ls /bin/ls‘)
    ‘/bin/ls‘
    """
    return getstatusoutput(cmd)[1]

题外话

其实python官方推荐使用这个函数执行系统命令
subprocess.run()

但是这个函数将输出直接print到终端中

如果需要屏蔽恣意的屏幕输出,可以使用subprocess.DEVNULL (subprocess 3.5.3中有)

# 即
x = subprocess.run(‘echo 250‘, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
# x:
# CompletedProcess(args=‘echo 250‘, returncode=0)

# reference: https://stackoverflow.com/questions/8529390/is-there-a-quiet-version-of-subprocess-call

原文地址:https://blog.51cto.com/l0vesql/2357311

时间: 2024-08-30 00:46:54

【python2】commands模块getstatusoutput函数的小问题的相关文章

[Python] 利用commands模块执行Linux shell命令

用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要函数: 1. commands.getoutput('shell command') 执行shell命令,返回结果(string类型) >>> commands.getoutput('pwd') '/home/oracle' 2. commands.getstatus('file') 该函数

python commands模块在python3.x被subprocess取代

subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 import commands result = commands.getoutput('cmd') #只返回执行的结果, 忽略返回值. result = commands.getstatus('cmd') #返回ls -ld file执行的结果. result = commands.getstat

python之commands模块

commands模块 用于执行Linux shell命令,要获得shell命令的输出只需要在后面参数写入('命令')就可以了. 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数:1). commands.getstatusoutput(命令) 执行shell命令, 返回两个元素的元组tuple(status, result),status为int类型,result为string类型. cmd命令的执行方式是{ cmd

python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: def getstatus(file): """Return output of "ls -ld <file>" in a string.""" import warnings warnings.warn("co

python标准库介绍——34 commands 模块详解

==commands 模块== (只用于 Unix) ``commands`` 模块包含一些用于执行外部命令的函数. [Example 3-7 #eg-3-7] 展示了这个模块. ====Example 3-7. 使用 commands 模块====[eg-3-7] ``` File: commands-example-1.py import commands stat, output = commands.getstatusoutput("ls -lR") print "s

Python的logging模块、os模块、commands模块与sys模块

一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message') logging.warning('This is warning message') 屏幕上打印: WARNING:root:This is warning message 默认情况下,logging将日志打印到屏幕,日志级别为WARNING: 日志级别大小关系为:CRITICAL > ERR

day17——logging、os模块、commands模块、sys模块

logging的使用 日志是我们排查问题的关键利器,写好日志记录,当我们发生问题时,可以快速定位代码范围进行修改.Python有给我们开发者们提供好的日志模块,我们我们就来介绍一下logging模块: 首先,我们先来看一个例子: import logging logging.debug("This is debug message") logging.info("The is info message") logging.warning("This is

获取VB类模块成员函数指针(转)

最近在做一些VB6.VBA的项目,被如何获取类模块中的函数指针这个问题所困扰,收集整理后,有2分资料值得收藏,特将关键部分留存,以备后续查找. 参照连接1:http://www.cnblogs.com/pctgl/articles/1352916.html 参照连接2:http://blog.csdn.net/lyserver/article/details/4224676 以下是链接1中的部分内容: 1. 函数地址 = GetClassProcAddress ( 指定为哪个函数 [上面解释],

python中的commands模块

commands模块用于调用shell命令 有3中方法: commands.getstatus()   返回执行状态 commands.getoutput()   返回执行结果 commands.getstatusoutput()  返回一个元组,执行状态和执行结果 其他执行shell命令的方法还有: 1.os.system(cmd) 2.os.popen(cmd)