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

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。

目录
相关文章
|
3天前
|
消息中间件 监控 网络协议
Python中的Socket魔法:如何利用socket模块构建强大的网络通信
本文介绍了Python的`socket`模块,讲解了其基本概念、语法和使用方法。通过简单的TCP服务器和客户端示例,展示了如何创建、绑定、监听、接受连接及发送/接收数据。进一步探讨了多用户聊天室的实现,并介绍了非阻塞IO和多路复用技术以提高并发处理能力。最后,讨论了`socket`模块在现代网络编程中的应用及其与其他通信方式的关系。
|
6天前
|
Python
Python 中常用的内置模块之`re`模块
【10月更文挑战第11天】 `re` 模块是 Python 内置的正则表达式处理工具,支持模式匹配、搜索、替换等功能。通过 `search`、`match`、`findall` 和 `sub` 等函数,结合正则表达式的元字符、分组、贪婪模式等特性,可高效完成文本处理任务。示例代码展示了基本用法,帮助快速上手。
9 1
|
6天前
|
JSON 数据格式 Python
Python基础-常用内置模块
【10月更文挑战第11天】 Python 内置模块丰富,涵盖系统交互、时间处理、数学运算、正则表达式、数据序列化等功能,如 `sys`、`os`、`time`、`datetime`、`random`、`math`、`re`、`json`、`pickle` 和 `csv` 等,极大提升了开发效率和代码质量。
8 1
|
10天前
|
Python
Python实用记录(四):os模块-去后缀或者改后缀/指定目录下图片或者子目录图片写入txt/csv
本文介绍了如何使用Python的os模块来操作文件,包括更改文件后缀、分割文件路径和后缀、将指定目录下的所有图片写入txt文档,以及将指定目录下所有子目录中的图片写入csv文档,并为每个子目录分配一个标签。
10 1
|
6天前
|
存储 C++ Python
[oeasy]python037_ print函数参数_sep分隔符_separator
本文介绍了Python中`print`函数的`sep`参数,即分隔符。通过回顾上文内容,解释了类型与`type`的概念,并强调了参数类型的重要性。文章详细探讨了`print`函数如何使用`sep`参数来分隔输出值,默认分隔符为空格(序号32)。还讨论了如何修改分隔符为其他字符,如冒号,并解释了为何反斜杠需要使用双反斜杠表示。最后,文章追溯了`sep`名称的由来,以及相关词汇的历史背景,如盎格鲁-萨克逊人的武器和语言。
12 0
|
7天前
|
机器学习/深度学习 缓存 Linux
python环境学习:pip介绍,pip 和 conda的区别和联系。哪个更好使用?pip创建虚拟环境并解释venv模块,pip的常用命令,conda的常用命令。
本文介绍了Python的包管理工具pip和环境管理器conda的区别与联系。pip主要用于安装和管理Python包,而conda不仅管理Python包,还能管理其他语言的包,并提供强大的环境管理功能。文章还讨论了pip创建虚拟环境的方法,以及pip和conda的常用命令。作者推荐使用conda安装科学计算和数据分析包,而pip则用于安装无法通过conda获取的包。
24 0
|
11天前
|
Python
Python中tqdm模块的常用方法和示例
`tqdm` 是一个快速、可扩展的Python进度条库,适用于长循环中添加进度提示。通过封装迭代器 `tqdm(iterator)`,可以轻松实现进度显示。支持自定义描述、宽度及嵌套进度条,适用于多种迭代对象。在Jupyter notebook中,可自动调整显示效果。
19 0
|
11天前
|
Python
Python中threading模块的常用方法和示例
Python 的 `threading` 模块提供了多线程编程的能力,允许同时执行多个线程。主要类包括 `Thread`、`Lock` 和 `Condition`。`Thread` 类用于创建和管理线程,`Lock` 用于同步线程,防止资源竞争,`Condition` 用于线程间协调。本文介绍了这些类的常用方法及示例代码,帮助你更好地理解和使用多线程编程。
19 0