大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

subprocess模块

1.subprocess模块介绍

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

成都创新互联公司成立于2013年,是专业互联网技术服务公司,拥有项目网站建设、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元细河做网站,已为上家服务,为细河各地企业和个人服务,联系电话:18982081108

"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
>>> 

文章题目:subprocess模块
当前地址:http://dzwzjz.com/article/geeoed.html
在线咨询
服务热线
服务热线:028-86922220
TOP