首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

subprocess.Popen怎么才能迅速显示命令输出的信息

2012-04-19 
subprocess.Popen如何才能迅速显示命令输出的信息Python code#import subprocessclass NetDetect:def __in

subprocess.Popen如何才能迅速显示命令输出的信息

Python code
#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()


ping命令的结果总是需要等待很长时间才能显示出来,如何才能迅速显示呢?

[解决办法]
上述代码稍微修改下,在XP系统下, python 3.2.2 版本下运行正常,代码如下所示:
Python code
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
[解决办法]
探讨

上述代码稍微修改下,在XP系统下, python 3.2.2 版本下运行正常,代码如下所示:
Python code

import subprocess

class NetDetect:

def __init__(self, addr):
self.addr = addr
self.log = open( 'NetDetect.log',……

热点排行