婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > 詳談python中subprocess shell=False與shell=True的區(qū)別

詳談python中subprocess shell=False與shell=True的區(qū)別

熱門標(biāo)簽:房產(chǎn)電銷外呼系統(tǒng) 315電話機(jī)器人廣告 地圖制圖標(biāo)注位置改變是移位嗎 南京銷售外呼系統(tǒng)軟件 上海機(jī)器人外呼系統(tǒng)哪家好 蓋州市地圖標(biāo)注 地圖標(biāo)注的意義點(diǎn) 浙江電銷卡外呼系統(tǒng)好用嗎 地圖標(biāo)注微信發(fā)送位置不顯示

shell=True參數(shù)會(huì)讓subprocess.call接受字符串類型的變量作為命令,并調(diào)用shell去執(zhí)行這個(gè)字符串,當(dāng)shell=False是,subprocess.call只接受數(shù)組變量作為命令,并將數(shù)組的第一個(gè)元素作為命令,剩下的全部作為該命令的參數(shù)。

舉個(gè)例子來說明

from subprocess import call  
import shlex  
cmd = "cat test.txt; rm test.txt"  
call(cmd, shell=True)

上述腳本中,shell=True的設(shè)置,最終效果是執(zhí)行了兩個(gè)命令

cat test.txt 和 rm test.txt

把shell=True 改為False,

from subprocess import call  
import shlex  
cmd = "cat test.txt; rm test.txt"  
cmd = shlex(cmd)  
call(cmd, shell=False)

則調(diào)用call的時(shí)候,只會(huì)執(zhí)行cat的命令,且把 "test.txt;" "rm" "test.txt" 三個(gè)字符串當(dāng)作cat的參數(shù),所以并不是我們直觀看到的好像有兩個(gè)shell命令了。

也許你會(huì)說,shell=True 不是很好嗎,執(zhí)行兩個(gè)命令就是我期望的呀。但其實(shí),這種做法是不安全的,因?yàn)槎鄠€(gè)命令用分號(hào)隔開,萬(wàn)一檢查不夠仔細(xì),執(zhí)行了危險(xiǎn)的命令比如 rm -rf / 這種那后果會(huì)非常嚴(yán)重,而使用shell=False就可以避免這種風(fēng)險(xiǎn)。

總體來說

看實(shí)際需要而定,官方的推薦是盡量不要設(shè)置shell=True。

補(bǔ)充: python subprocess模塊的shell參數(shù)問題

昨天調(diào)試其他同學(xué)的代碼時(shí),發(fā)現(xiàn)對(duì)于subprocess模塊所傳的args變量,與shell變量存在關(guān)聯(lián),傳值不當(dāng)會(huì)有各種問題。比較有趣,就記錄一下。

根據(jù)subprocess模塊的args定義如下:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

對(duì)于args,可傳string,也可傳list,但當(dāng)傳string時(shí),shell的值必須設(shè)為True。

當(dāng)shell為True時(shí)

If shell is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user's home directory.

就是調(diào)用了系統(tǒng)的 sh 來執(zhí)行命令(args的string),這樣會(huì)導(dǎo)致一些猥瑣的安全問題,類似于SQL Injection攻擊:

from subprocess import call
filename = input("What file would you like to display?\n")
What file would you like to display?
non_existent; rm -rf / #
call("cat " + filename, shell=True) # Uh-oh. This will end badly...

所以,安心用shell=False吧,記得args傳list。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • Python中判斷subprocess調(diào)起的shell命令是否結(jié)束
  • python3通過subprocess模塊調(diào)用腳本并和腳本交互的操作
  • python subprocess pipe 實(shí)時(shí)輸出日志的操作
  • 通過實(shí)例解析python subprocess模塊原理及用法
  • 使用python執(zhí)行shell腳本 并動(dòng)態(tài)傳參 及subprocess的使用詳解
  • python中的subprocess.Popen()使用詳解
  • 解決python subprocess參數(shù)shell=True踩到的坑

標(biāo)簽:雙鴨山 陽(yáng)泉 克拉瑪依 貴州 金華 日照 赤峰 臨汾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳談python中subprocess shell=False與shell=True的區(qū)別》,本文關(guān)鍵詞  詳談,python,中,subprocess,shell,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳談python中subprocess shell=False與shell=True的區(qū)別》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于詳談python中subprocess shell=False與shell=True的區(qū)別的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 黄平县| 昌江| 商水县| 万州区| 泸溪县| 景宁| 松溪县| 青冈县| 湘西| 资阳市| 卓尼县| 麻江县| 通山县| 塔河县| 贵州省| 五原县| 深泽县| 宾阳县| 皋兰县| 体育| 衡水市| 宁远县| 攀枝花市| 西昌市| 喀喇| 金湖县| 昌都县| 资阳市| 莆田市| 任丘市| 达孜县| 台中市| 琼海市| 措美县| 新绛县| 分宜县| 新巴尔虎左旗| 临清市| 腾冲县| 蓬溪县| 苗栗市|