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
  • 相关阅读:
    网络流(平面图转对偶图)
    666
    期望总结
    docker-1-简介
    22、整合mybatis
    21、整合Druid数据源
    20、Springboot 与数据访问(JDBC/自动配置)
    19、配置嵌入式servlet容器(下)
    18、配置嵌入式servlet容器(2)
    17、配置嵌入式servlet容器(1)
  • 原文地址:https://www.cnblogs.com/lutt/p/15808860.html
Copyright © 2011-2022 走看看