subprocess模块-创新互联

1.subprocess模块介绍

我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本的模块在python2有os.system,,commands,popen2等也可以,比较乱,于是官方推出了subprocess,目地是提供统一的模块来实现对系统命令或脚本的调用

创新互联是专业的双城网站建设公司,双城接单;提供成都做网站、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行双城网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
"os.system获取不到返回值"
>>> import os
>>> a = os.system("df -h") 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  1.9G   97G   2% /
devtmpfs        479M     0  479M   0% /dev
tmpfs           489M     0  489M   0% /dev/shm
tmpfs           489M  6.7M  482M   2% /run
tmpfs           489M     0  489M   0% /sys/fs/cgroup
/dev/sda1      1014M  120M  895M  12% /boot
tmpfs            98M     0   98M   0% /run/user/0
>>> a
0
" os.popen可以获取到返回值"
>>> os.popen("df -h")

>>> f = os.popen("df -h")
>>> f.read()
'Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3        98G  1.9G   97G   2% /\ndevtmpfs        479M     0  479M   0% /dev\ntmpfs           489M     0  489M   0% /dev/shm\ntmpfs           489M  6.7M  482M   2% /run\ntmpfs           489M     0  489M   0% /sys/fs/cgroup\n/dev/sda1      1014M  120M  895M  12% /boot\ntmpfs            98M     0   98M   0% /run/user/0\n'

"python2中的commands模块"
>>> import commands
>>> commands.getstatusoutput("df -h")
(0, 'Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3        98G  1.8G   97G   2% /\ndevtmpfs        479M     0  479M   0% /dev\ntmpfs           489M     0  489M   0% /dev/shm\ntmpfs           489M  6.7M  482M   2% /run\ntmpfs           489M     0  489M   0% /sys/fs/cgroup\n/dev/sda1      1014M  120M  895M  12% /boot\ntmpfs            98M     0   98M   0% /run/user/0')

2.subprocess执行命令

2.1sbuprocess执行命令的三种方法

subprocess.run(*popenargs, input=None, timeout=None, check=False, **kwargs) #官方推荐

subprocess.call(*popenargs, timeout=None, **kwargs) #跟上面实现的内容差不多,另一种写法

subprocess.Popen() #上面各种方法的底层封装
"subprocess.run"
>>> a = subprocess.run(["df","-h"])
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  2.3G   96G   3% /
devtmpfs        479M     0  479M   0% /dev
tmpfs           489M     0  489M   0% /dev/shm
tmpfs           489M  6.8M  482M   2% /run
tmpfs           489M     0  489M   0% /sys/fs/cgroup
/dev/sda1      1014M  120M  895M  12% /boot
tmpfs            98M     0   98M   0% /run/user/0
>>> a.args
['df', '-h']
>>> a.returncode
0
>>> a.check_returncode()

>>> a = subprocess.run(["df","-h"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a.stdout
b'Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3        98G  2.3G   96G   3% /\ndevtmpfs        479M     0  479M   0% /dev\ntmpfs           489M     0  489M   0% /dev/shm\ntmpfs           489M  6.8M  482M   2% /run\ntmpfs           489M     0  489M   0% /sys/fs/cgroup\n/dev/sda1      1014M  120M  895M  12% /boot\ntmpfs            98M     0   98M   0% /run/user/0\n'
>>> a.stderr
b''

>>> a = subprocess.run(["dsf","-h"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/python3Dir/lib/python3.6/subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/python3Dir/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/local/python3Dir/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dsf': 'dsf'

>>> a = subprocess.run(["df","-h","|","grep","a"],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> a.stdout      
b'Filesystem     1K-blocks    Used Available Use% Mounted on\n/dev/sda3      102709252 2337100 100372152   3% /\ndevtmpfs          490020       0    490020   0% /dev\ntmpfs             499848       0    499848   0% /dev/shm\ntmpfs             499848    6900    492948   2% /run\ntmpfs             499848       0    499848   0% /sys/fs/cgroup\n/dev/sda1        1038336  121872    916464  12% /boot\ntmpfs              99972       0     99972   0% /run/user/0\n'
"subprocess.call"
>>> a = subprocess.call(["df","-h"])
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        98G  2.3G   96G   3% /
devtmpfs        479M     0  479M   0% /dev
tmpfs           489M     0  489M   0% /dev/shm
tmpfs           489M  6.8M  482M   2% /run
tmpfs           489M     0  489M   0% /sys/fs/cgroup
/dev/sda1      1014M  120M  895M  12% /boot
tmpfs            98M     0   98M   0% /run/user/0
>>>
"subprocess.Popen会发起新的进程,不影响主进程"
>>> a = subprocess.Popen("sleep 10",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)#10秒后返回
>>> a = subprocess.Popen("sleep 10",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)#立刻返回,poll()可以用来监控命令是否结束
>>> a.poll()
>>> a.poll()
>>> a.poll()
0
"运行函数"
>>> def sayhi():
    print("hello")... 
... 
>>> sayhi()
hello
>>> a = subprocess.Popen("echo $PWD",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,preexec_fn=sayhi)
>>> a.stdout.read()
b'hello\n/usr/local/python3/Python-3.6.3\n'
>>> 
>>> a = subprocess.Popen("echo $PWD",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,cwd="/tmp",preexec_fn=sayhi)
>>> a.stdout.read()
b'hello\n/tmp\n'
>>> 
>>> a = subprocess.Popen("echo $PWD;sleep 15",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,cwd="/tmp",preexec_fn=sayhi)
>>> a.wait() #等在这里直到上面的程序运行完成
0
>>> 
"Popen程序的终止"
>>> a = subprocess.Popen("echo $PWD;sleep 100",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> a.terminate()#程序的终止
>>> a.kill())#程序的终止
>>> 
>>> import signal
>>> a = subprocess.Popen("echo $PWD;sleep 100",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> a.send_signal(signal.SIGKILL)
"communicate()程序交互,只能交互一次,再次交互会报错"
>>> import subprocess
>>> a = subprocess.Popen("python3 guessage.py",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> a.communicate(b'33')

(b'input the age of you think:you should input one number!\ninput the age of you think:you should input one number!\ninput the age of you think:you should input one number!\n', b'')
>>> 
>>> a.communicate(b'12')
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/python3Dir/lib/python3.6/subprocess.py", line 818, in communicate
    raise ValueError("Cannot send input after starting communication")
ValueError: Cannot send input after starting communication
>>> 

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:subprocess模块-创新互联
URL分享:http://myzitong.com/article/dooopd.html