subprocess.Popen如何才能迅速显示命令输出的信息
#import subprocessclass NetDetect: def __init__(self, addr): self.addr = addr self.log = open( 'NetDetect.log', 'w' ) if self.log == None: print 'Open NetDetect.log failed' def __del__(self): if self.fdp: self.fdp.kill() if self.log: self.log.close() def ping(self): self.fdp = subprocess.Popen( [ 'ping', self.addr, '-t' ], stdout = subprocess.PIPE ) for line in self.fdp.stdout: print line, self.log.write( line ) self.fdp = Noneif __name__ == '__main__' : nd = NetDetect( 'www.sina.com.cn' ) nd.ping()
import subprocessclass NetDetect: def __init__(self, addr): self.addr = addr self.log = open( 'NetDetect.log', 'w' ) if self.log == None: print('Open NetDetect.log failed') def __del__(self): if self.fdp: self.fdp.kill() if self.log: self.log.close() def ping(self): self.fdp = subprocess.Popen( [ 'ping', self.addr, '-t' ], stdout = subprocess.PIPE ) for line in self.fdp.stdout: print(line.decode("gb2312")) self.log.write(line.decode("gb2312")) self.log.flush() self.fdp = Noneif __name__ == '__main__' : nd = NetDetect( 'www.sina.com.cn' ) nd.ping()
[解决办法]
嗯,3版的可以跑,还用2版嘛就试试下面dev版本...
http://code.google.com/p/subprocdev/source/browse/subprocess.py?name=default
[解决办法]