subprocess模块

subprocess模块

subprocess主要用于提供执行系统命令的功能,其在python3中将commands、os.system,os.popen等功能全部集成了,所以在py3中推荐使用该模块
下面是可以执行shell命令的相关模块和函数:

os.system
os.spawn*
os.popen* --废弃
popen2.* --废弃
commands.* --废弃,3.x中被移除

常用方法

  • subprocess.call() 执行命令,取出命令执行的结果,通常是shell返回的状态码,注意call会把执行结果打印出来,但是只会取执行结果的状态码
>>> import subprocess
>>> res = subprocess.call('df -h',shell=True)
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda3              19G  9.4G  8.2G  54% /
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/dev/vda1             194M   32M  153M  18% /boot
>>> print(res)
0
>>> res = subprocess.call(['df', '-h'],shell=False)
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda3              19G  9.4G  8.2G  54% /
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/dev/vda1             194M   32M  153M  18% /boot
>>> print(res)
0
>>> res = subprocess.call('123',shell=True)
/bin/sh: 123: command not found
>>> print(res)
127

补充:
shell = True ,允许 shell 命令是字符串形式,否则,必须按列表形式传参数
  • subprocess.check_call() 执行命令,如果执行状态码是 0 ,则返回0,否则抛异常。注意,如果命令执行成功,则subprocess.check_call()为0,否则subprocess.check_call()1没有值
>>> res1 = subprocess.check_call('free -m',shell=True)
             total       used       free     shared    buffers     cached
Mem:          7870       5217       2653          0        488       3108
-/+ buffers/cache:       1620       6250
Swap:         1023          0       1023
>>> print(res1)
0
>>> res2 = subprocess.check_call('123',shell=True)
/bin/sh: 123: command not found
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3/lib/python3.5/subprocess.py", line 584, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '123' returned non-zero exit status 127
>>> print(res2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'res2' is not defined
  • check_output 执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常.
>>> res = subprocess.check_output('df -h',shell=True)
>>> print(res)
b'Filesystem            Size  Used Avail Use% Mounted on
/dev/vda3              19G  9.4G  8.2G  54% /
tmpfs                 3.9G     0  3.9G   0% /dev/shm
/dev/vda1             194M   32M  153M  18% /boot
'
>>> res1 = subprocess.check_output('dfaa',shell=True)
/bin/sh: dfaa: command not found
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3/lib/python3.5/subprocess.py", line 629, in check_output
    **kwargs).stdout
  File "/usr/local/python3/lib/python3.5/subprocess.py", line 711, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'dfaa' returned non-zero exit status 127
>>> print(res1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'res1' is not defined
  • subprocess.Popen 执行复杂的命令

    • Popen类:
class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
  • 参数说明:

    • args:shell命令,可以是字符串或者序列类型(如:list,元组)
    • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
    • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    • close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    • shell:同上
    • cwd:用于设置子进程的当前目录
    • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    • universal_newlines:不同系统的换行符不同,True -> 同意使用
    • startupinfo与createionflags只在windows下有效将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等

    • 执行普通命令:

>>> child = subprocess.Popen(['ping','-c','4','proxy.fuzengjie.cn'])
>>> PING proxy.fuzengjie.cn (23.83.227.252) 56(84) bytes of data.
64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=1 ttl=48 time=188 ms
64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=2 ttl=48 time=184 ms
64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=3 ttl=48 time=184 ms
64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=4 ttl=48 time=184 ms

--- proxy.fuzengjie.cn ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3273ms
rtt min/avg/max/mdev = 184.661/185.637/188.307/1.544 ms

>>> 

从运行结果中看到,父进程在开启子进程之后并没有等待child的完成,而是直接运行print
还可以获取子进程的状况
* child.poll() # 检查子进程状态
* child.kill() # 终止子进程
* child.send_signal() # 向子进程发送信号
* child.terminate() # 终止子进程
* child.pid #子进程的pid

  • 管道

可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe)

import subprocess

obj = subprocess.Popen(["python3"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
obj.stdin.write("print asd")
obj.stdin.close()

cmd_out = obj.stdout.read()
obj.stdout.close()
cmd_error = obj.stderr.read()
obj.stderr.close()


print(123)
print(cmd_out)
print(cmd_error)

输出:
123

  File "<stdin>", line 2
    print asd
            ^
SyntaxError: Missing parentheses in call to 'print'

subprocess.PIPE实际上为文本流提供一个缓存区。stdin 将标准输入放到缓冲区,系统执行完之后,会把标准输出和标准错误输出放到缓冲区,这样直接可以从缓冲区里拿出结果

  • 父子进程通信
>>> import subprocess
>>> child1 = subprocess.Popen('netstat -lnpt',shell=True,stdout=subprocess.PIPE)
>>> child2 = subprocess.Popen(["grep","80"],stdin=child1.stdout, stdout=subprocess.PIPE)
>>> print(child2.stdout.read())
b'tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      9804/nginx          
'
>>> print(child2.stdout.read())
b''
>>> child1 = subprocess.Popen('netstat -lnpt',shell=True,stdout=subprocess.PIPE)
>>> child2 = subprocess.Popen(["grep","80"],stdin=child1.stdout, stdout=subprocess.PIPE)
>>> output = child2.communicate()
>>> print(output)
(b'tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      9804/nginx          
', None)
>>> print(output)
(b'tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      9804/nginx          
', None)
>>> output = child2.communicate()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3/lib/python3.5/subprocess.py", line 1055, in communicate
    stdout = self.stdout.read()
ValueError: read of closed file

child1的stdout将文本输出到缓存区,随后child2的stdin从该PIPE中将文本读取走。child2的输出文本也被存放在PIPE中,直到communicate()方法从PIPE中读取出PIPE中的文本、注意:communicate()是Popen对象的一个方法,该方法会阻塞父进程,直到子进程完成。

原文地址:https://www.cnblogs.com/pycode/p/subprocess.html