首页 > 代码库 > [蟒蛇菜谱] Python调用shell命令,获取返回值和返回信息

[蟒蛇菜谱] Python调用shell命令,获取返回值和返回信息

# -*- coding: utf-8 -*-import osimport subprocessimport signalclass MockLogger(object):    ‘‘‘模拟日志类。方便单元测试。‘‘‘    def __init__(self):        self.info = self.error = self.critical = self.debug    def debug(self, msg):        print "__LOGGER__:"+msg        class Shell(object):    ‘‘‘完成Shell脚本的包装。    run()为普通调用,会等待shell命令返回。    run_background()为异步调用,会立刻返回,不等待shell命令完成    异步调用时,可以使用get_status()查询状态,或使用wait()进入阻塞状态,    等待shell执行完成。    异步调用时,使用kill()强行停止脚本后,仍然需要使用wait()等待真正退出。    TODO 未验证Shell命令含有超大结果输出时的情况。    ‘‘‘    def __init__(self, cmd):        self.cmd = cmd  # cmd包括命令和参数        self.ret_code = None        self.ret_info = None        self.err_info = None        #使用时可替换为具体的logger        self.logger = MockLogger()             def run_background(self):        ‘‘‘以非阻塞方式执行shell命令(Popen的默认方式)。        ‘‘‘        self.logger.debug("run %s"%self.cmd)        # Popen在要执行的命令不存在时会抛出OSError异常,但shell=True后,        # shell会处理命令不存在的错误,因此没有了OSError异常,故不用处理        self._process = subprocess.Popen(self.cmd, shell=True,                 stdout=subprocess.PIPE, stderr=subprocess.PIPE) #非阻塞            def run(self):        ‘‘‘以阻塞方式执行shell命令。        ‘‘‘        self.run_background()        self.wait()    def wait(self):        ‘‘‘等待shell执行完成。        ‘‘‘        self.logger.debug("waiting %s"%self.cmd)        self.ret_info, self.err_info = self._process.communicate() #阻塞        # returncode: A negative value -N indicates that the child was         # terminated by signal N        self.ret_code = self._process.returncode        self.logger.debug("waiting %s done. return code is %d"%(self.cmd,                             self.ret_code))    def get_status(self):        ‘‘‘获取脚本运行状态(RUNNING|FINISHED)        ‘‘‘        retcode = self._process.poll()        if retcode == None:            status = "RUNNING"        else:            status = "FINISHED"        self.logger.debug("%s status is %s"%(self.cmd, status))        return status    # Python2.4的subprocess还没有send_signal,terminate,kill    # 所以这里要山寨一把,2.7可直接用self._process的kill()     def send_signal(self, sig):        self.logger.debug("send signal %s to %s"%(sig, self.cmd))        os.kill(self._process.pid, sig)    def terminate(self):        self.send_signal(signal.SIGTERM)    def kill(self):        self.send_signal(signal.SIGKILL)if __name__ == "__main__":    ‘‘‘test code‘‘‘    # 1. test normal    sa = Shell(‘who‘)    sa.run()    print "return code:", sa.ret_code    print "return info:", sa.ret_info        # 2. test stderr    sb = Shell(‘ls /export/dir_should_not_exists‘)    sb.run()    print "return code:", sb.ret_code    print "return info:", sb.ret_info    print "error info:", sb.err_info        # 3. test background    sc = Shell(‘sleep 2‘)    sc.run_background()    print ‘warmly hello from parent process‘    print "return code:", sc.ret_code    print "status:", sc.get_status()    sc.wait()    print "return code:", sc.ret_code    print "return info:", sc.ret_info        # 4. test kill    import time    sd = Shell(‘sleep 2‘)    sd.run_background()    time.sleep(1)    sd.kill()    sd.wait() # NOTE, still need to wait    print sd.ret_code    print sd.ret_info        # 5. test multiple command and uncompleted command output    se = Shell(‘pwd;sleep 1;pwd;pwd‘)    se.run_background()    time.sleep(1)    se.kill()    se.wait() # NOTE, still need to wait    print se.ret_code    print se.ret_info        # 6. test wrong command    sf = Shell(‘aaaaa‘)    sf.run()    print sf.ret_code    print sf.ret_info    print sf.err_info

 

[蟒蛇菜谱] Python调用shell命令,获取返回值和返回信息