Python中Subprocess的不同函数解析
作者:Taosiyu 时间:2022-03-10 05:23:00
以前我一直用os.system()处理一些系统管理任务,因为我认为那是运行linux命令最简单的方式.
我们能从Python官方文档里读到应该用subprocess 模块来运行系统命令.subprocess模块允许我们创建子进程,连接他们的输入/输出/错误管道,还有获得返回值。
subprocess模块打算来替代几个过时的模块和函数,比如: os.system, os.spawn*, os.popen*, popen2.*命令。
让我们来看一下subprocess 有哪些不同的函数.
subprocess.call()
执行由参数提供的命令.
我们可以用数组作为参数运行命令,也可以用字符串作为参数运行命令(通过设置参数shell=True)
注意,参数shell默认为False
我们用subprocess.call()来做一个统计磁盘的例子:
subprocess.call(['df', '-h'])
下面的例子把shell设置为True
subprocess.call('du -hs $HOME', shell=True)
注意,python官方文档里对参数shell=True陈述了一个警告:
Invoking the system shell with shell=True can be a security hazard if combined
with untrusted input
现在,我们来看看输入与输出
Input and Output
subprocess 模块能阻止输出,当你不关心标准输出的时候是非常方便的.
它也使你通过一种正确的方式管理输入/输出,有条理地整合python脚本中的的shell命令.
Return Codes
通过subprocess.call的返回值你能够判定命令是否执行成功.
每一个进程退出时都会返回一个状态码,你可以根据这个状态码写一些代码。
stdin, stdout and stderr
在使用subprocess 时,有一个微妙的部分是怎么使用管道把命令连接起来.
管道表明一个新的子管道应该被创建.
默认的设置为None,意味着没有重定向发生
标准错误可以指向标准输出,表明子进程的错误信息会被捕获到和标准输出同一个文件.
subprocess.Popen()
subprocess 模块中基本的进程创建和管理由Popen 类来处理.
subprocess.popen是用来替代os.popen的.
我们来做一些真实的例子,subprocess.Popen需要一个数组作为参数:
import subprocess
p = subprocess.Popen(["echo", "hello world"], stdout=subprocess.PIPE)
print p.communicate()
>>>('hello world
', None)
注意,虽然你可以使用 "shell=True",但并不推荐这样的方式.
如果你知道你只用几个有限的函数,比如Popen和PIPE,你可以单单指定这几个函数:
from subprocess import Popen, PIPE
p1 = Popen(["dmesg"], stdout=PIPE)
print p1.communicate()
Popen.communicate()
communicate()函数返回一个tuple(标准输出和错误).
Popen.communicate() 和进程沟通:发送数据到标准输入.从标准输出和错误读取数据直到遇到结束符.等待进程结束.
输入参数应该是一个字符串,以传递给子进程,如果没有数据的话应该是None.
基本上,当你用 communicate()函数的时候意味着你要执行命令了.
用subprocess写Ping程序
我们先问用户地址,然后用ping请求这个地址.
# Import the module
import subprocess
# Ask the user for input
host = raw_input("Enter a host to ping: ")
# Set up the echo command and direct the output to a pipe
p1 = subprocess.Popen(['ping', '-c 2', host], stdout=subprocess.PIPE)
# Run the command
output = p1.communicate()[0]
print output
来源:https://www.cnblogs.com/taosiyu/p/12012612.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现的各种常见分布算法示例
![](https://img.aspxhome.com/file/2023/1/76061_0s.png)
Python进度条的制作代码实例
Python利用FlashText算法实现替换字符串
![](https://img.aspxhome.com/file/2023/3/82563_0s.png)
使用Python制作一个数据预处理小工具(多种操作一键完成)
![](https://img.aspxhome.com/file/2023/8/59828_0s.jpg)
已安装tensorflow-gpu,但keras无法使用GPU加速的解决
![](https://img.aspxhome.com/file/2023/6/81736_0s.jpg)
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
python如何删除字符串最后一个字符
python中日期和时间格式化输出的方法小结
ASP和Javascript中取整函数的应用
c++生成dll使用python调用dll的方法
python实现截取屏幕保存文件,删除N天前截图的例子
利用python+ffmpeg合并B站视频及格式转换的实例代码
asp如何显示自定义随机信息?
python numpy元素的区间查找方法
详解python分布式进程
使用Alt提升可访问性
![](https://img.aspxhome.com/file/UploadPic/20094/4/alt_flickr_addrss-44s.png)
Django重装mysql后启动报错:No module named ‘MySQLdb’的解决方法
![](https://img.aspxhome.com/file/2023/3/64253_0s.png)