Why can't I execute another python script using the subprocess module via a webserver

两盒软妹~` 提交于 2019-12-13 16:37:35

问题


Having a problem envoking another script, by the way it works fine from the console when I just call python main.py (which then calls test3.py) but when I do it via the webserver it gives the error below which is cryptic

I can't call this (test3 is just a print...)

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

but this works fine

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

Error Log in /var/log/httpd-error.log on FreeBSD

[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

回答1:


The python executable is not in the webservers executable PATH. The webserver may also be secured with a chroot or similar techniques and therefore be unable to access the python installation.

Try specifying the full path of the python executable (you can find it out interactively with the which command), like this:

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



回答2:


  1. Could you directly import your test3 code in to the project instead of calling it through the OS?

  2. Are your relative paths all correct? It kind of sounds like you might be running something from the wrong directory or missing some needed files in the directory on the remote machine.



来源:https://stackoverflow.com/questions/9521055/why-cant-i-execute-another-python-script-using-the-subprocess-module-via-a-webs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!