tags:

views:

789

answers:

3

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.

+4  A: 
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.

Pablo Santa Cruz
ok, will this kill my php script if i'll quit python while php script is still running?
Phil
Why do I need to type 15 characters when the correct answer is "yes"? :-)So: Yes, it will.
Lennart Regebro
ok... see my question edit - i'm searching for something that will let php script continue its task even if python will already finish
Phil
A: 

I'll paraphrase the answer to http://stackoverflow.com/questions/1060436/how-do-i-include-a-php-script-in-python.

import subprocess

def php(script_path):
    p = subprocess.Popen(['php', script_path] )
S.Lott
Thanks. Sorry for noob question, but since it's on remote server how do I specify path? Can it be url? Also, will php script continue working even if python will quit?
Phil
+2  A: 

If python is on a different physical machine than the PHP script, I'd make sure the PHP script is web-accessible and use urllib2 to call to that url

import urllib2

urllib2.urlopen("http://remotehost.com/myscript.php")
Mark Biek
Finally something useful! Will have to check if it works, but if it works it's exactly what I want to do. Thank you.
Phil