小陆同学

python 中文名:蟒蛇,设计者:Guido van Rossum

导航

python实现linux命令结果输出

def sh(self,cmds, print_msg=True):
    print('sh')
    msgStr = []
    env = ###dev###
    compilePopen = subprocess.Popen(cmds,shell=True,stdout=subprocess.PIPE,close_fds=True,stderr=subprocess.STDOUT)
    returnMsg = ''
    for line in iter(compilePopen.stdout.readline, b''):
        line = line.rstrip().decode('utf8')

        if line.find('ERROR') != -1 or line.find('Err') != -1:
            msg = '<div style="color: red">'+line+'</div>'
        elif line.find('WARN') != -1:
            msg = '<div style="color: #66512c">' + line + '</div>'
        elif line.find('SUCCESS') != -1:
            msg = '<div style="color: green">' + line + '</div>'
        else:
            msg = line
        returnMsg = returnMsg + msg + '\n'
        if print_msg:
            time_now = time.strftime("%H:%M:%S", time.localtime()) if self.apps.deploying_showTime=='true' else ''
            self.ws.send(json.dumps({
                "progress": "post_deploy",
                "msg": time_now +'    '+ msg,
                'ctime': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                'user': self.task.username
            }))
    # result = base.cmds(cmds)
    stdout, stderr = compilePopen.communicate()
    returncode = compilePopen.returncode
    if returncode > 0:
        result = {"status": "failed", "msg": returnMsg}
    result =  {"status": "succeed", "msg": returnMsg}
    self.handle_result("msg", result)

 

posted on 2022-01-16 10:30  小陆同学  阅读(413)  评论(0编辑  收藏  举报