[英]subprocess.Popen raise child Exception
I put an operation in my python file : 我在python文件中添加了一个操作:
subprocess.Popen(['notify-send', message])
and the error on terminal is : 并且终端上的错误是:
subprocess.Popen(['notify-send', message])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__ errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
how to prevent this error? 如何防止这个错误?
Popen uses default no environment, so not PATH. Popen使用默认的无环境,因此不使用PATH。 There are several solutions:
有几种解决方案:
subprocess.Popen(args=['notify-send', message], env={'PATH': os.getenv('PATH')}
subprocess.Popen(args=['notify-send', message], env={'PATH': os.getenv('PATH')}
进程中使用环境subprocess.Popen(args=['notify-send', message], env={'PATH': os.getenv('PATH')}
notify-send
subprocess.Popen(['/full/path/here/notify-send', message])
notify-send
subprocess.Popen(['/full/path/here/notify-send', message])
By design, programs starting other programs do not use the PATH environment variable by default. 根据设计,默认情况下,启动其他程序的程序不使用PATH环境变量。 It used to be a common attack way to subvert a legit program by changing the PATH or by installing a rogue program with a higher priority in normal PATH than the real child.
过去,这是通过更改PATH或通过在正常PATH中安装比真实孩子更高优先级的恶意程序来破坏合法程序的常见攻击方式。 As a result, the rogue program could be executed on behalf on the user without any abnormal action from him/her.
结果,可以代表用户执行恶意程序,而无需他/她采取任何异常动作。
There are tons of ways of forcing the use of the PATH and it is fine for simple operations (parameter shell = TRUE
is one of them). 有成千上万种强制使用PATH的方法,对于简单的操作来说很好(参数
shell = TRUE
是其中之一)。 But for more serious scripts it gives the same difference as using the frowned upon system
instead of fork
+ exec
in C or C++ languages. 但是对于更严重的脚本,它的使用效果与使用皱眉
system
而不是使用C或C ++语言的fork
+ exec
。
TL/DR: the most correct way is to pass the full PATH of the child program. TL / DR:最正确的方法是传递子程序的完整PATH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.