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

主頁 > 知識庫 > 詳談python中subprocess shell=False與shell=True的區別

詳談python中subprocess shell=False與shell=True的區別

熱門標簽:房產電銷外呼系統 315電話機器人廣告 地圖制圖標注位置改變是移位嗎 南京銷售外呼系統軟件 上海機器人外呼系統哪家好 蓋州市地圖標注 地圖標注的意義點 浙江電銷卡外呼系統好用嗎 地圖標注微信發送位置不顯示

shell=True參數會讓subprocess.call接受字符串類型的變量作為命令,并調用shell去執行這個字符串,當shell=False是,subprocess.call只接受數組變量作為命令,并將數組的第一個元素作為命令,剩下的全部作為該命令的參數。

舉個例子來說明

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

上述腳本中,shell=True的設置,最終效果是執行了兩個命令

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)

則調用call的時候,只會執行cat的命令,且把 "test.txt;" "rm" "test.txt" 三個字符串當作cat的參數,所以并不是我們直觀看到的好像有兩個shell命令了。

也許你會說,shell=True 不是很好嗎,執行兩個命令就是我期望的呀。但其實,這種做法是不安全的,因為多個命令用分號隔開,萬一檢查不夠仔細,執行了危險的命令比如 rm -rf / 這種那后果會非常嚴重,而使用shell=False就可以避免這種風險。

總體來說

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

補充: python subprocess模塊的shell參數問題

昨天調試其他同學的代碼時,發現對于subprocess模塊所傳的args變量,與shell變量存在關聯,傳值不當會有各種問題。比較有趣,就記錄一下。

根據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.

對于args,可傳string,也可傳list,但當傳string時,shell的值必須設為True。

當shell為True時

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.

就是調用了系統的 sh 來執行命令(args的string),這樣會導致一些猥瑣的安全問題,類似于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。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

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

標簽:雙鴨山 陽泉 克拉瑪依 貴州 金華 日照 赤峰 臨汾

巨人網絡通訊聲明:本文標題《詳談python中subprocess shell=False與shell=True的區別》,本文關鍵詞  詳談,python,中,subprocess,shell,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳談python中subprocess shell=False與shell=True的區別》相關的同類信息!
  • 本頁收集關于詳談python中subprocess shell=False與shell=True的區別的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 肥城市| 阳信县| 团风县| 沾化县| 大城县| 县级市| 邵武市| 德令哈市| 广州市| 峨边| 紫云| 左权县| 诏安县| 南江县| 博湖县| 安岳县| 海原县| 共和县| 五原县| 柳林县| 金川县| 乐陵市| 瑞丽市| 胶州市| 中西区| 池州市| 渝北区| 兰西县| 徐汇区| 奈曼旗| 五寨县| 道真| 澜沧| 赣州市| 绥化市| 曲阜市| 永年县| 竹溪县| 外汇| 新绛县| 敦煌市|