Python之subprocess模块的使用
1、subprocess调用系统的命令
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess
import sys completed = subprocess.run(['ls',sys.argv[1],'-l'])
print('运行结果',completed.returncode)
subprocess_os_system.py
运行效果
[root@ mnt]# python3 subprocess_os_system.py /mnt/
total
-rw-r--r-- root root Dec : subprocess_os_system.py
2、subprocess利用shell进程运行命令
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess completed=subprocess.run('echo $HOME',shell=True) print('执行返回码:',completed.returncode)
subprocess_shell_variables.py
运行效果
[root@ mnt]# python3 subprocess_shell_variables.py
/root
执行返回码:
3、subprocess错误的处理
(1)、check=True,会检查退出码,如果返回非0,则抛出subprocess.CalledProcessError异常
(2)、check=False,不会检查退出码
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess try:
subprocess.run(['false'], check=True) except subprocess.CalledProcessError as err:
print('运行错误:', err)
subprocess_run_check.py
运行效果
[root@ mnt]# python3 subprocess_run_check.py
运行错误: Command '['false']' returned non-zero exit status .
4、subprocess管道显示运行结果
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess
import sys completed = subprocess.run(['ls', sys.argv[1], '-l'], stdout=subprocess.PIPE)
print('completed.returncode: ',completed.returncode)
print('completed.stdout: \n',str(completed.stdout,'utf-8'))
subprocess_run_output.py
运行效果
[root@ mnt]# python3 subprocess_run_output.py /mnt/
completed.returncode:
completed.stdout:
total
-rw-r--r-- root root Dec : subprocess_run_output.py
5、 subprocess管道显示以及异常的捕捉
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess try:
completed = subprocess.run('echo to stdout;'
'echo to stderr 1>&2;'
'exit 1',
check=True,
shell=True,
stdout=subprocess.PIPE)
except subprocess.CalledProcessError as err:
print('执行错误: ', err)
else:
print('completed.returncode: ', completed.returncode)
print('completed.stdout: ', str(completed.stdout, encoding='utf-8'))
subprocess_run_output_error.py
运行效果
[root@ mnt]# python3 subprocess_run_output_error.py
to stderr
执行错误: Command 'echo to stdout;echo to stderr 1>&2;exit 1' returned non-zero exit status .
6、subprocess管道显示和管道错误信息的显示
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess try:
completed = subprocess.run('echo to stdout;'
'echo to stderr 1>&2;'
'exit 1',
check=False,#如果check=False,则不抛出 subprocess.CalledProcessError 异常
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except subprocess.CalledProcessError as err:
print('执行错误: ', err)
else:
print('completed.returncode: ', completed.returncode)
print('completed.stdout: ', completed.stdout.decode('utf-8'))
print('completed.stderr: ', completed.stderr.decode('utf-8'))
subprocess_run_output_error_trap.py
运行效果
[root@ mnt]# python3 subprocess_run_output_error_trap.py
completed.returncode:
completed.stdout: to stdout completed.stderr: to stderr
7、subprocess之check_output函数,实现错误信息与命令执行结果一起输出显示
import subprocess try:
completed = subprocess.check_output('echo to stdout; echo to stderr 1>&2;',
shell=True,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
print('执行错误: ', err)
else:
print('completed.returncode: ', completed.returncode)
print('completed.stdout: ', str(completed.stdout, encoding='utf-8'))
subprocess_run_output_error_trap.py
运行效果
[root@ mnt]# python3 subprocess_run_output_error_trap.py
completed: b'to stdout\nto stderr\n'
completed.stdout: to stdout
to stderr
8、subprocess抑制输出,相当于把标准输出或标准错误写入/dev/null
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess try:
completed = subprocess.run('echo to stdout; echo to stderr 1>&2;',
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError as err:
print('执行错误: ', err)
else:
print('completed.returncode: ', completed.returncode)
print('completed.stdout: ', completed.stdout)
print('completed.stderr: ', completed.stderr)
subprocess_run_output_error_suppress.py
运行效果
[root@ mnt]# python3 subprocess_run_output_error_suppress.py
completed.returncode:
completed.stdout: None
completed.stderr: None
8、subprocess直接使用管道处理读取
由于call()、run()、check_output()都是Popen类的装饰器,直接使用Popen更容易的控制命令执行结果的标准的输入输出
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess proc = subprocess.Popen(['echo', 'Popen 输出'],
stdout=subprocess.PIPE, )
stdout_value = proc.communicate()[0].decode('utf-8')
print('stdout:', repr(stdout_value)) #返回一个对象的 string 格式。
subprocess_popen_read.py
运行效果
[root@ mnt]# python3 subprocess_popen_read.py
stdout: 'Popen 输出\n'
9、subprocess直接使用管道处理写数据
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess print('write:')
proc = subprocess.Popen(
['cat', '-'],
stdin=subprocess.PIPE,
)
proc.communicate('我写入数据\n'.encode('utf-8'))
subprocess_popen_write.py
运行效果
[root@ mnt]# python3 subprocess_popen_write.py
write:
我写入数据
10、 Popen进程双向通道,实现同时完成标准的读写
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess proc = subprocess.Popen(
['cat', '-'],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE
) msg = '我是标准输入的内容 stdin'.encode('utf-8')
stdout_value = proc.communicate(msg)[0].decode('utf-8')
print('stdout_value: ', stdout_value)
subprocess_popen2.py
运行效果
[root@ mnt]# python3 subprocess_popen2.py
stdout_value: 我是标准输入的内容 stdin
11、Popen捕获错误输出,实现标准错误信息的显示
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess proc = subprocess.Popen(
'cat -;echo "to stderr" 1>&2',
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE
) msg = '我是标准输入的内容 stdin'.encode('utf-8')
stdout_value, stderr_value = proc.communicate(msg)
print('stdout_value: ', repr(stdout_value.decode('utf-8')))
print('stderr_value: ', repr(stderr_value.decode('utf-8')))
subprocess_popen3.py
运行效果
[root@ mnt]# python3 subprocess_popen3.py
stdout_value: '我是标准输入的内容 stdin'
stderr_value: 'to stderr\n'
12、Popen把标准错误切换标准输出显示
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess proc = subprocess.Popen(
'cat -;echo "to stderr" 1>&2',
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT
) msg = '我是标准输入的内容 stdin'.encode('utf-8')
stdout_value, stderr_value = proc.communicate(msg)
print('stdout_value: ', repr(stdout_value.decode('utf-8')))
print('stderr_value: ', repr(stderr_value))
subprocess_popen4.py
运行效果
[root@ mnt]# python3 subprocess_popen4.py
stdout_value: '我是标准输入的内容 stdinto stderr\n'
stderr_value: None
13、Popen管道之间的连接,相当于Linux命令管道传送
如 linux命令
[root@ mnt]# cat cz.txt | grep | head -n
用python实现如上命令的方法
#!/usr/bin/env python
# -*- coding: utf-8 -*- import subprocess cat = subprocess.Popen(
['cat', '/mnt/cz.txt'],
stdout=subprocess.PIPE,
) grep = subprocess.Popen(
['grep', ''],
stdin=cat.stdout,
stdout=subprocess.PIPE,
) head = subprocess.Popen(
['head', '-n', ''],
stdin=grep.stdout,
stdout=subprocess.PIPE,
) result_stdout = head.communicate()[0].decode('utf-8') print(result_stdout)
subprocess_pipes.py
运行效果
[root@ mnt]# python3 subprocess_pipes.py
14、Popen命令之间的交互
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#中继程序 import sys sys.stderr.write('repeater.py: starting\n')
sys.stderr.flush() while True:
next_line = sys.stdin.readline()
sys.stderr.flush()
if not next_line:
break
sys.stdout.write(next_line)
sys.stdout.flush() sys.stderr.write('repeater.py: exiting\n')
sys.stderr.flush()
repeater.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#交互的程序 import io
import subprocess print('#####一次一行输出#####') # 创建一个Popen对象,执行的命令结果通过管道传输
proc = subprocess.Popen(
'python3 /mnt/repeater.py',
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
) # 创建输入的缓冲区,设置每发一次数据,单独一行
stdin = io.TextIOWrapper(
proc.stdin,
encoding='utf-8',
line_buffering=True, # send data on newline
) # 创建输出的缓冲区
stdout = io.TextIOWrapper(
proc.stdout,
encoding='utf-8',
) for i in range(5):
line = '{}\n'.format(i)
stdin.write(line) # 写入缓冲区
output = stdout.readline() # 从输出缓冲区读取一行
print(output.rstrip()) # 去除右边的空格
remainder = proc.communicate()[0].decode('utf-8') # 读取管理的结果
print(remainder) print('#####一次全部输出#####')
proc = subprocess.Popen(
'python3 /mnt/repeater.py',
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
stdin = io.TextIOWrapper(
proc.stdin,
encoding='utf-8',
)
for i in range(5):
line = '{}\n'.format(i)
stdin.write(line)
stdin.flush() output = proc.communicate()[0].decode('utf-8')
print(output)
interaction.py
运行效果
[root@ mnt]# python3 repeater.py #开启中继程序监听数据收和发
repeater.py: starting [root@ mnt]# python3 interaction.py
#####一次一行输出#####
repeater.py: starting repeater.py: exiting #####一次全部输出#####
repeater.py: starting
repeater.py: exiting
15、利于模块signal实现进程间信号的传递,即是利父程去管理子进程,使用os.kill关闭子进程未运行完成的程序
#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import signal
import time
import sys pid = os.getpid()
received = False def signal_usr1(signum, frame):
"Callback invoked when a signal is received"
global received
received = True
print('CHILD {:>6}: Received USR1'.format(pid))
sys.stdout.flush() print('CHILD {:>6}: 设置信号处理程序'.format(pid))
sys.stdout.flush()
signal.signal(signal.SIGUSR1, signal_usr1) print('CHILD {:>6}: 暂停等待信号'.format(pid))
sys.stdout.flush()
time.sleep(5) if not received:
print('CHILD {:>6}: 从未接收到信号'.format(pid))
signal_child.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import signal
import subprocess
import time
import sys proc = subprocess.Popen(['python3', 'signal_child.py']) print('PARENT : Pausing before sending signal...') sys.stdout.flush() time.sleep(1) print('PARENT : Signaling child') sys.stdout.flush() os.kill(proc.pid, signal.SIGUSR1)
signal_parent.py
运行结果
[root@ mnt]# python3 signal_parent.py
PARENT : Pausing before sending signal...
CHILD : 设置信号处理程序
CHILD : 暂停等待信号
PARENT : Signaling child
CHILD : Received USR1
16、进程组和会话
问题:如果使用Popen创建了子进程,那么创建的子进程就不会接收到父进程的信号,问题代码如下:
import os
import signal
import subprocess
import tempfile
import time
import sys script = '''#!/bin/sh
echo "Shell script in process $$"
set -x
python3 /mnt/signal_child.py
'''
#在/tmp目录下,将script写入一个临时文件
script_file = tempfile.NamedTemporaryFile('wt')
script_file.write(script)
script_file.flush() proc = subprocess.Popen(['sh', script_file.name]) #script_file.name ==> /tmp/tmprizk8l2p print('PARENT : Pausing before signaling {}...'.format(proc.pid))
sys.stdout.flush() time.sleep(1) print('PARENT : Signaling child {}'.format(proc.pid))
sys.stdout.flush() os.kill(proc.pid, signal.SIGUSR1)
time.sleep(3)
subprocess_signal_parent_shell.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import signal
import time
import sys pid = os.getpid()
received = False def signal_usr1(signum, frame):
"Callback invoked when a signal is received"
global received
received = True
print('CHILD {:>6}: Received USR1'.format(pid))
sys.stdout.flush() print('CHILD {:>6}: 设置信号处理程序'.format(pid))
sys.stdout.flush()
signal.signal(signal.SIGUSR1, signal_usr1) print('CHILD {:>6}: 暂停等待信号'.format(pid))
sys.stdout.flush()
time.sleep(3) if not received:
print('CHILD {:>6}: 从未接收到信号'.format(pid))
signal_child.py
运行效果
[root@ mnt]# python3 subprocess_signal_parent_shell.py
Shell script in process
+ python3 /mnt/signal_child.py
PARENT : Pausing before signaling ...
CHILD : 设置信号处理程序
CHILD : 暂停等待信号
PARENT : Signaling child
CHILD : 从未接收到信号
#以上由Popen创建了子进程shell,shell解释器又创建进程调用signal_child.py程序,所以产生进程id不一样,导致进程之间无法相互发信号通讯。
利用进程组之间的通信,解决进程之间pid识别不到,导致信号传输失败的问题
#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import signal
import subprocess
import tempfile
import time
import sys def show_setting_prgrp():
print('Calling os.setpgrp() from {}'.format(os.getpid()))
os.setpgrp()
print('Process group is now {}'.format(os.getpgrp()))
sys.stdout.flush() script = '''#!/bin/sh
echo "Shell script in process $$"
set -x
python3 /mnt/signal_child.py
'''
script_file = tempfile.NamedTemporaryFile('wt')
script_file.write(script)
script_file.flush() proc = subprocess.Popen(
['sh', script_file.name],
preexec_fn=show_setting_prgrp,
) print('PARENT : Pausing before signaling {}...'.format(proc.pid))
sys.stdout.flush()
time.sleep(1) print('PARENT : Signaling process group {}'.format(proc.pid))
sys.stdout.flush() os.killpg(proc.pid, signal.SIGUSR1)
time.sleep(3)
subprocess_signal_setpgrp.py
运行效果
[root@ mnt]# python3 subprocess_signal_setpgrp.py
Calling os.setpgrp() from
Process group is now
Shell script in process
+ python3 /mnt/signal_child.py
PARENT : Pausing before signaling ...
CHILD : 设置信号处理程序
CHILD : 暂停等待信号
PARENT : Signaling process group
CHILD : Received USR1
Python之subprocess模块的使用的更多相关文章
- Python中subprocess 模块 创建并运行一个进程
python的subprocess模块,看到官方声明里说要尽力避免使用shell=True这个参数,于是测试了一下: from subprocess import call import shlex ...
- python的subprocess模块(写的不错留作查询)
python的subprocess模块 subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*. ...
- python 利用python的subprocess模块执行外部命令,获取返回值
有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...
- python之subprocess模块详解--小白博客
subprocess模块 subprocess是Python 2.4中新增的一个模块,它允许你生成新的进程,连接到它们的 input/output/error 管道,并获取它们的返回(状态)码.这个模 ...
- python中subprocess模块
subprocess 模块 subprocess称之为子进程,进程是一个正在进行的程序 子进程是由另一个正在运行的程序启动的程序,例如QQ聊天点击一个链接,打开了浏览器,那么浏览器称之为QQ的子进程 ...
- python的subprocess模块执行shell命令
subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...
- Python之Subprocess模块
PS:打开文件时候加b参数是代表以二进制方式打开,在Linux加不加都可以,在windows上面最好加b参数否则可能会出现问题 使用system返回执行结果不赋值,使用popen返回了结果赋值给cmd ...
- Python之subprocess模块、sys模块
一.subprocess模块 # import os # os.system('tasklist') #类似cmd输入系统命令 ''' subprocess的目的就是启动一个新的进程并且与之通信. s ...
- python基础--subprocess模块
可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 commands.* ...
- Python的subprocess模块(二)
原文:http://blog.chinaunix.net/uid-26000296-id-4461522.html 一.subprocess 模块简介 subprocess最早是在2.4版本中引入的. ...
随机推荐
- FileSystemResource 找不到文件
环境 Spring 3.2.5.RELEASE 原因 使用 FileSystemResource 加载文件的过程中,发现一个奇怪的现象,路径完全正确,但是找不到文件的情况.可能的原因是文件的路径上有压 ...
- DLL导出函数
使用DEF文件从DLL导出 模块定义(.def)文件时包含一个或多个描述DLL各种属性的Module语句的文本文件.如果不使用_declspec(dllexport)关键字导出DLL的函数,则DLL需 ...
- django ajax MTV与MVC 多对多创建方式
MTV与MVC MTV模型(django): M:模型层(models.py) T:templates V:views MVC模型: M:模型层(models.py) V:视图层(views.py) ...
- Phython-守护线程
import threading,time def run(n): print("task is ",n) time.sleep(2) print("task done& ...
- 算法:二叉树的层次遍历(递归实现+非递归实现,lua)
二叉树知识参考:深入学习二叉树(一) 二叉树基础 递归实现层次遍历算法参考:[面经]用递归方法对二叉树进行层次遍历 && 二叉树深度 上面第一篇基础写得不错,不了解二叉树的值得一看. ...
- WAMPSERVER-服务器离线无法切换到在线状态问题的解决
问题描述:WAMPSERVER-服务器离线“切换到在线状态”则弹出: 解决方案: 本地连接---属性----Internet 协议(TCP/IP)---高级---wins----导入LMHOSTS( ...
- MFC 下调试 出现 warning : fail to load indicator string 0x0069
MFC 下调试 出现 warning : fail to load indicator string 0x0069 就是程序状态栏每一个标识列中至少有一个值没有初始值 或初始值为空 导致程序没有获取到 ...
- upload上传 和 download下载
文件上传: <div class="upload-form"> <input id="fileUpload" type="fil ...
- vue中的常用三元
点击事件的三元 <el-button type="primary" @click="edit == 'mod' ? sureModify() : submit()& ...
- Java高并发程序设计学习笔记(四):无锁
转自:https://blog.csdn.net/dataiyangu/article/details/86440836#1__3 1. 无锁类的原理详解简介:1.1. CAS1.2. CPU指令2. ...