相关疑难解决方法(0)

何时对Python子进程模块使用Shell = True

似乎每当我尝试使用Python的子进程模块时,我发现我仍然不理解某些东西.目前,我试图从Python模块中加入3 mp4文件.

当我尝试

z ='MP4Box -cat test_0.mp4 -cat test_1.mp4 -cat test_2.mp4 -new test_012d.mp4'
subprocess.Popen(z,shell=True)
Run Code Online (Sandbox Code Playgroud)

一切正常.

当我尝试

z = ['MP4Box', '-cat test_0.mp4', '-cat test_1.mp4', '-cat test_2.mp4', '-new test_012d.mp4']
subprocess.Popen(z,shell=False)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Option -cat test_0.mp4 unknown. Please check usage
Run Code Online (Sandbox Code Playgroud)

我认为,因为shell=False我只需要提供一个列表,其中第一个元素是我想要运行的可执行文件,每个后续元素都是该可执行文件的参数.我是否误解了这个信念,或者是否有正确的方法来创建我想要使用的命令?

另外,有没有Shell=True在subprocess.Popen中使用的规则?到目前为止,我真正知道的(?)是"不要这样做 - 你可以将你的代码暴露给Shell注入攻击".为什么要Shell=False避免这个问题?使用'Shell = True`是否真的有优势?

python shell subprocess

4
推荐指数
2
解决办法
8274
查看次数

带有变量的Python子进程调用

我目前正在为客户编写脚本。

该脚本从配置文件读取。然后将其中一些信息存储在变量中。

之后,我想使用subprocess.call执行安装命令,所以我正在使用这些变量来构建安装命令

call("mount -t cifs //%s/%s %s -o username=%s" % (shareServer, cifsShare, mountPoint, shareUser))
Run Code Online (Sandbox Code Playgroud)

但是这不起作用

Traceback (most recent call last):
  File "mount_execute.py", line 50, in <module>
    main()
  File "mount_execute.py", line 47, in main
    call("mount -t cifs //%s/%s %s -o username=%s" % (shareServer, cifsShare, mountPoint, shareUser))
  File "/usr/lib64/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib64/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1141, in _execute_child
   raise child_exception
 OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

首先使用以下命令 …

python subprocess python-2.6

3
推荐指数
1
解决办法
1953
查看次数

标签 统计

python ×2

subprocess ×2

python-2.6 ×1

shell ×1