python cmd设定超时支持window和linux - 在线代码运行


            
执行结果

名称:python cmd设定超时支持window和linux

当前代码

#!/usr/bin/python
# -*- coding:utf8 -*-
import os  
import platform  
import subprocess  
import signal  
import time  

def command(cmd, timeout=60):  
    """Run command and return the output 
    cmd - the command to run 
    timeout - max seconds to wait for 
    """  
    is_linux = platform.system() == 'Linux'  
      
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid if is_linux else None)  
    t_beginning = time.time()  
    seconds_passed = 0  
    while True:  
        if p.poll() is not None:  
            break  
        seconds_passed = time.time() - t_beginning  
        if timeout and seconds_passed > timeout:  
            if is_linux:  
                os.killpg(p.pid, signal.SIGTERM)  
            else:  
                p.terminate()  
            raise TimeoutError(cmd, timeout)  
        time.sleep(0.1)  
    return p.stdout.read()  
if __name__ == '__main__':     
    try:  
        result = command('ping baidu.com', timeout=2)  
    except Exception:  
        print 'Run command timeout.'  
    else:  
        print result