详谈python中subprocess shell=False与shell=True的区别

作者:逍遥自在017 时间:2021-08-01 02:00:01 

shell=True参数会让subprocess.call接受字符串类型的变量作为命令,并调用shell去执行这个字符串,当shell=False是,subprocess.call只接受数组变量作为命令,并将数组的第一个元素作为命令,剩下的全部作为该命令的参数。

举个例子来说明

详谈python中subprocess shell=False与shell=True的区别


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。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/xiaoyaozizai017/article/details/72794469

标签:python,shell,False,True
0
投稿

猜你喜欢

  • 下一站:HandlerSocket!

    2011-04-11 09:02:00
  • 设置SQLServer数据库中某些表为只读的多种方法分享

    2012-07-11 15:41:05
  • 在函数间不能传递32个以上参数的疑难问题

    2008-12-31 13:31:00
  • Django中F函数的使用示例代码详解

    2023-11-21 00:56:06
  • 以独占方式打开Access数据库

    2007-10-22 12:24:00
  • Google的用户体验设计原则

    2009-01-12 18:31:00
  • python ansible自动化运维工具执行流程

    2021-08-07 01:54:25
  • python中adb有什么功能

    2022-06-22 13:25:15
  • ASP.NET技巧:同时对多个文件进行大量写操作对性能优化

    2023-07-08 12:22:37
  • Python实现约瑟夫环问题的方法

    2021-09-07 19:41:28
  • js 轮播效果实例分享

    2023-07-16 00:42:43
  • 在ASP.NET 2.0中操作数据之十二:在GridView控件中使用TemplateField

    2023-07-07 07:02:50
  • pycharm新建一个python工程步骤

    2023-08-22 17:42:54
  • prototype-1.4.0注释版源代码下载

    2007-09-30 14:06:00
  • python神经网络Inception ResnetV2模型复现详解

    2023-02-28 08:51:35
  • Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

    2023-10-31 09:31:29
  • 不同操作系统下的mysql数据库同步

    2008-12-22 14:41:00
  • python DataFrame中loc与iloc取数据的基本方法实例

    2022-06-23 15:58:25
  • .NET 6中为record类型自定义Equals方法

    2023-07-15 21:29:29
  • 纯CSS Tooltips提示

    2008-10-18 16:01:00
  • asp之家 网络编程 m.aspxhome.com