zoukankan      html  css  js  c++  java
  • 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)
    python 中文名:蟒蛇,设计者:Guido van Rossum
  • 相关阅读:
    【JAVA】BitSet的源码研究
    【JAVA】大整数数据量排序
    三层架构的DCOM配置
    .NET+COM+组件注册方法
    开博感言
    about Cache
    代码HTML
    取远程用户IP地址
    ShowModalDialog页面传值
    about char变量
  • 原文地址:https://www.cnblogs.com/lutt/p/15808860.html
Copyright © 2011-2022 走看看