python - 为什么我不能通过网络服务器使用 subprocess 模块执行另一个 python 脚本

标签 python cgi-bin subprocess

调用另一个脚本时遇到问题,顺便说一下,当我调用 python main.py (然后调用 test3.py)时,它在控制台上工作正常,但是当我通过网络服务器执行此操作时,它会给出以下错误神秘

我不能调用这个(test3 只是一个打印...)

#proc = subprocess.Popen(['python', 'test3.py'], stdout=subprocess.PIPE)

但这工作正常

proc = subprocess.Popen(['ls', '-la'], stdout=subprocess.PIPE)

在 FreeBSD 上错误登录/var/log/httpd-error.log

[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] Traceback (most recent call   last):
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]   File "/usr/local/www/apache22/data/main2.py", line 22, in <module>
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] proc = subprocess.Popen(['python', 'test3.py'], stdout=subprocess.PIPE)
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]   File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] errread, errwrite)
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]   File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] raise child_exception
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] OSError
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] :
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] [Errno 2] No such file or directory
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61]
[Thu Mar 01 12:26:55 2012] [error] [client 64.102.194.61] File does not exist: /usr/local/www/apache22/data/favicon.ico

最佳答案

python 可执行文件不在网络服务器中 executable PATH 。网络服务器也可以通过 chroot 进行保护。或类似技术,因此无法访问 python 安装。

尝试指定 python 可执行文件的完整路径(您可以使用 which 命令交互地找到它),如下所示:

proc = subprocess.Popen(['/usr/bin/python', 'test3.py'], stdout=subprocess.PIPE)

关于python - 为什么我不能通过网络服务器使用 subprocess 模块执行另一个 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521055/

相关文章:

python - 多进程和子进程有什么区别?

python - 禁止读取/dev/tty

python - 如何使用 pygame 制作游戏中的命令控制台

Python字符串插值: only show necessary decimal places

web-applications - 如何从我的 URL 中删除 "cgi-bin"?

Python os 模块用相对路径打开当前目录上方的文件

python - 如何等待子进程完成、存储其输出并向输出添加前缀(不带新行)?

python - % 在 Django 中的日期前签名

python - 如何将一维数组转换为逻辑矩阵

php - 如何从网页在服务器上执行脚本?