Python 当我们使用 browsermobproxy中的Server.stop()时,发现无法完全关闭,因为无法结束java进程,在工作中最后需要quit() 关闭整个窗口时,无法关闭。以下记录一下处理方法:
找到 server.py文件并修改如下:(注意注释部分)
def stop(self):
"""
This will stop the process running the proxy
"""
if self.process.poll() is not None:
return
try:
self.process.kill()
self.process.wait()
###添加以下内容
find_port = 'netstat -aon|findstr %s' % self.port
result = os.popen(find_port)
text = result.read()
pid_line = text.split('\n', 1)[0]
pid = pid_line.replace(" ","").split("LISTENING")[1]
find_kill= 'taskkill -f -pid %s' % pid
result = os.popen(find_kill)
cmd = result.read()
cmd.close()
###添加以上内容
except AttributeError:
# kill may not be available under windows environment
pass
这样,在使用 server.stop()就可以把相关的进程清理干净啦!