企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] <br> ### subprocess 模块 Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system、os.spawn*、os.popen*、popen2.*、commands.*不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息。 #### subprocess.call(*popenargs, timeout=None, **kwargs) 执行命令,等待命令完成或超时,然后再返回状态码 ```cmd >>> subprocess.call("netstat -an | findstr 80",shell=True) TCP 192.168.0.106:60838 121.41.82.44:80 TIME_WAIT TCP 192.168.0.106:60839 121.41.82.44:80 TIME_WAIT UDP 127.0.0.1:58045 *:* UDP 127.0.0.1:58046 *:* 0 ``` #### subprocess.Popen(...) 在一个新进程中执行子程序,具体参数请查看源码 ```cmd >>> netstat = subprocess.Popen("netstat -an | findstr 80", shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> netstat <subprocess.Popen object at 0x000002965A2EE780> >>> netstat.stdout.read() b' TCP 192.168.0.106:60865 52.230.85.180:443 ESTABLISHED\r\n TCP 192.168.0.106:60943 140.207.124.210:80 LAST_ACK\r\n TCP 192.168.0.106:60950 121.41.82.44:80 TIME_WAIT\r\n TCP 192.168.0.106:60951 121.41.82.44:80 TIME_WAIT\r\n UDP 127.0.0.1:58045 *:* \r\n UDP 127.0.0.1:58046 *:* \r\n' >>> netstat.stderr.read() b'' >>> ``` <hr style="margin-top:100px"> :-: ![](https://box.kancloud.cn/2ff0bc02ec938fef8b6dd7b7f16ee11d_258x258.jpg) ***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***