0

What are the bad points when you execute a python script with php?

Also, how is it different from executing python through the cgi method

I found an this interesting method from Running python script from php and i thought it will be great to just use the

exec("python ../cgi-bin/form.py");

and closely-related methods.

Please explain properly and tell me what we have to keep in mind when using this method.

Community
  • 1
  • 1
Lok Jun
  • 1,315
  • 2
  • 9
  • 16

1 Answers1

1

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:

  • 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() or system() 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.

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.

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.

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.

Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Is it a server side script or is it something else? – Lok Jun Dec 26 '13 at 09:39
  • PHP is executed on server. So what do you mean? – Alma Do Dec 26 '13 at 09:40
  • Can you tell me about using cgi to execute vs php? – Lok Jun Dec 26 '13 at 09:40
  • If you need to launch something from PHP script (and that is strict requirement) - then `exec()`-like functions are your choice. I didn't get what you've mean under "CGI" in this context. There will be no difference since it will be web-server who'll launch executable (thus, permissions level will be same in common case) – Alma Do Dec 26 '13 at 09:41
  • You havent compared it with cgi in your answer. – Lok Jun Dec 26 '13 at 09:43