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
  • 相关阅读:
    test
    莫烦Python
    资源汇总
    AutoHotkey学习资源
    神器AutoHotkey学习(官方文档翻译)
    linux内核编译时bad register name `%dil'错误
    Linux内核修炼之道-->原作者博客链接--oschina备份
    vim笔记--oschina备份
    vim相关资源--oschina备份
    Windows下装Gvim时可能发生的错误--oschina备份
  • 原文地址:https://www.cnblogs.com/lutt/p/15808860.html
Copyright © 2011-2022 走看看