Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run (remote) php script from (local) python script

Tags:

python

php

How do I make python (local) run php script on a remote server?

I don't want to process its output with python script or anything, just execute it and meanwhile quit python (while php script will be already working and doing its job).

edit: What I'm trying to achieve:

  • python script connects to ftp server and uploads php script (I already have this part of code)
  • it runs php script (that's part of code i'm asking about)
  • python script continues to do something else
  • python script quits (but probably php script still didn't finished its work so i don't want it to end when it'll exit python)
  • python script quit, php script still continues its task

(I don't plan to do anything with php output in python - python just has to upload php script and make it start working)

Hope I'm more clear now. Sorry if my question wasn't specific enough.

another edit: Also please note that I don't have shell access on remote server. I have only ftp and control panel (cpanel); trying to use ftp for it.

like image 753
Phil Avatar asked Jul 09 '09 13:07

Phil


People also ask

How do I run a PHP server from python?

To run Python Script in PHP, we use “shell_exec” which returns all of the output streams as a string. The shell executes it, and the result can be returned as a string. It returns an error or no output at all if an empty value is passed.

How do I run a local python script on a remote machine?

Answer #2: Using the paramiko library – a pure python implementation of SSH2 – your python script can connect to a remote host via SSH, copy itself (!) to that host and then execute that copy on the remote host. Stdin, stdout and stderr of the remote process will be available on your local running script.

Can we connect PHP with python?

The simplest solution would be to use PHP's exec function. Of course this is with assumption that the Python code accept command line arguments and gives its output in stdout (and/or stderr). If the Python itself is another standalone web service, then the solution would be different.


1 Answers

os.system("php yourscript.php")

Another alternative would be:

# will return new process' id
os.spawnl(os.P_NOWAIT, "php yourscript.php")

You can check all os module documentation here.

like image 141
Pablo Santa Cruz Avatar answered Oct 31 '22 13:10

Pablo Santa Cruz