日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
subprocess模塊-創(chuàng)新互聯(lián)

1.subprocess模塊介紹

我們經(jīng)常需要通過Python去執(zhí)行一條系統(tǒng)命令或腳本,系統(tǒng)的shell命令是獨(dú)立于你的python進(jìn)程之外的,每執(zhí)行一條命令,就是發(fā)起一個(gè)新進(jìn)程,通過python調(diào)用系統(tǒng)命令或腳本的模塊在python2有os.system,,commands,popen2等也可以,比較亂,于是官方推出了subprocess,目地是提供統(tǒng)一的模塊來實(shí)現(xiàn)對系統(tǒng)命令或腳本的調(diào)用

創(chuàng)新互聯(lián)是專業(yè)的雙城網(wǎng)站建設(shè)公司,雙城接單;提供成都做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行雙城網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
"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執(zhí)行命令

2.1sbuprocess執(zhí)行命令的三種方法

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

subprocess.call(*popenargs, timeout=None, **kwargs) #跟上面實(shí)現(xiàn)的內(nèi)容差不多,另一種寫法

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會發(fā)起新的進(jìn)程,不影響主進(jìn)程"
>>> 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()可以用來監(jiān)控命令是否結(jié)束
>>> a.poll()
>>> a.poll()
>>> a.poll()
0
"運(yùn)行函數(shù)"
>>> 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() #等在這里直到上面的程序運(yùn)行完成
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()程序交互,只能交互一次,再次交互會報(bào)錯(cuò)"
>>> 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
>>> 

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


標(biāo)題名稱:subprocess模塊-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.dlmjj.cn/article/dooopd.html