栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python subprocess模块命令执行过程及示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python subprocess模块命令执行过程及示例

python subprocess模块命令执行过程及示例 命令简介

subprocess模块能够生成子进程,连接到它们的输入/输出/错误管道,并获取它们的返回码。
通过该模块,可以实现python脚本执行shell命令。
常见用法是subprocess.run(),subprocess.Popen()能够提供更高级的特性,下文以Popen为例进行介绍。

class subprocess.Popen(args, bufsize=- 1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, umask=- 1, encoding=None, errors=None, text=None, pipesize=- 1)

args是程序参数,可以一个参数序列,也可以是字符串。 默认情况下,如果 args 是一个序列,则要执行的程序是 args 中的第一项。
参数序列格式:

subprocess.Popen(['ls', '-l'], shell=True)

字符串格式:

subprocess.Popen("ls -l", shell=True)

shell 参数决定了是否使用 shell 来执行,若shell=True,shell默认使用/bin/sh,要想使用/bin/bash来执行命令的话,需要指定executable=‘/bin/bash’

命令执行过程

为更好看清在python中调用subprocess.Popen()的详细过程,使用如下代码作为范例:
subprocess_test.py

import time
import subprocess

s = subprocess.Popen(["sleep 50"], shell=True)
print("start")
print(s.pid)
time.sleep(20)
s.kill()
print("finish")

运行subprocess_test.py,输出如下:

start
2474
finish                                                                                                             

在finish之前,执行ps -auxf > log.txt,查看详细的执行情况
log.txt

terry     5573  0.0  0.0  24368  6440 pts/3    Ss   Jun25   0:00      _ -bash
terry     2471  0.0  0.0  29112  9632 pts/3    T    13:40   0:00          _ python3.8 subprocess_test.py                                                   
terry     2474  0.0  0.0   4636   860 pts/3    T    13:40   0:00          |   _ /bin/sh -c sleep 50                                                                        
terry     2475  0.0  0.0   7932   880 pts/3    T    13:40   0:00          |       _ sleep 50                                                                               
terry     3870  0.0  0.0  40100  3568 pts/3    R+   13:40   0:00          _ ps -auxf                  

能够看出,python程序并不是直接运行 sleep 50,实际上先派生出了/bin/sh子进程,再由sh执行sleep命令;而且打印出的pid号也正是/bin/sh的pid。

获得标准输出、标准错误、返回码

Popen同样提供了communicate函数来和派生出的子进程进行交互。
Popen选项可以控制stdin、stdout、stderr的输出方式,stdin、stdout、stderr一旦被赋值为subprocess.PIPE,表示需建立一条连通标准流的管道,可以从pipe中读取流。

Popen.communicate(input=None, timeout=None)

input选项可以将数据发送给子进程,input数据的格式可以是string或者bytes。

communicate的返回值是一个(stdout_data, stderr_data)元组。

使用范例:

res = subprocess.Popen('ls -l',stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,executable='/bin/bash')
stdout, stderr = res.communicate()
if stdout != None:                                                                                                                                                              
   print(stdout)                                                                                                                                                           
if res.returncode:                                                                                                                                                                
   print(stderr)                                          
   sys.exit(int(res.returncode))   
参考

https://docs.python.org/3/library/subprocess.html
https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号