怎么在Python中使用Subprocess解析不同的函数

发布时间:2021-02-19 16:57:52 作者:Leah
来源:亿速云 阅读:151

本篇文章给大家分享的是有关怎么在Python中使用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

以上就是怎么在Python中使用Subprocess解析不同的函数,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. python中的subprocess.Popen()使用详解
  2. subprocess模块怎么在Python中使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python subprocess

上一篇:如何在python中区分不同的数据类型

下一篇:怎么在Apache中配置多个监听端口

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》