问题描述
使用php执行python脚本时的坏处是什么?
What are the bad points when you execute a python script with php?
此外,它与通过cgi方法执行python有何不同
Also, how is it different from executing python through the cgi method
我从并且我认为只使用
exec("python ../cgi-bin/form.py");
和密切相关的方法。
请正确解释,并告诉我使用此方法时要记住什么。
Please explain properly and tell me what we have to keep in mind when using this method.
推荐答案
您的问题很常见-并且通常,这与执行 python 脚本无关-而是执行一些 external 命令。为此,您需要满足一些条件:
You problem is very common - and in general it's not about executing python scripts - but to execute some external commands. To do that, you'll need some conditions to be fulfilled:
- 通常,PHP是由网络服务器运行的。因此,要执行某些脚本,Web服务器必须能够做到这一点。这意味着-从中启动Web服务器的 OS用户必须具有足够的权限才能执行外部命令
- 在许多情况下,外部执行功能(例如或被视为不安全-因此通常被禁用案例(我说的是托管)。因此,依靠这些功能将使您的应用程序的技术要求更加严格-并且您将无法使用此类托管。
- Normally, PHP is run by web-server. So to execute some script, web-server must be able to do that. It means - that OS user, from which web-server was launched, must have enough permissions to execute external command
- In many cases, external execution functions, like
exec()
orsystem()
are treated as unsafe - and, thus, are disabled in common case (I'm speaking about hostings). So, relying on those functions will make your application's technical requirements more strict - and you'll not be able to use such hostings.
除上述内容外,PHP脚本将挂起,直到完整数据从 exec()
传递回脚本。这意味着执行速度慢且响应时间低。而且,在Win系统中,外部脚本的执行时间到脚本的总执行时间(与* nix不同)系统),因此,如果您的外部脚本响应时间太长,您很有可能会捕获到时限错误。
Besides described above, PHP script will "hang" until full data will be passed from exec()
back to script. That means slow execution and low-predictable response-time. And, more, in Win systems execution of external scripts is included to total script execution time (unlike in *nix systems) - and, therefore, you may have good chances to catch time limit error if your external script was too long to response.
如果您想进行一些比较 ,将python脚本作为CGI启动-那么您应该选择CGI。至少因为它旨在达到该目的。使用CGI启动python脚本绝对可以赢得速度-因为启动PHP脚本没有任何开销(例如,如果您只想使用python,则可以禁用PHP支持)。在通常情况下,权限级别不会有问题,因为在结束时,将从Web服务器用户启动可执行文件,因此,在两种情况下它们都是相同的。而且,正如我上面提到的,通过CGI启动不会将您绑定到PHP的时间限制-您不会在乎PHP发生了什么。
If you want to make some "comparison" with launching python script as CGI - then you should choose CGI. At least because it's intended to serve that purpose. Launching python script with CGI will definitely win in terms of speed - because there will be no overhead for launching PHP script (and you may, for example, disable PHP support if you want to only use python). Permissions level in common case will not be a problem, since, at end point, executable will be launched from web-server user, thus, they will be same in both cases. And, as I've mentioned above, launching via CGI will not bind you to PHP's time limits - you'll not care about what's happening in PHP.
所以主要思想是-是的,这是一种启动方式。但是,不-如果您可以通过CGI启动以本机方式进行操作,那不是应该做的事情。
So the main idea is - yes, it is a way to launch. But no - that's not a thing that you should do if you can do that natively via CGI launch.
这篇关于php exec python脚本'弱点'/缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!