5

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.

Phil
  • 3,628
  • 8
  • 34
  • 36
  • i don't want to include it and use its output, i want to do equivalent of what happens when user enters php script's address in browser – Phil Jul 09 '09 at 14:21
  • @Phil, please update your question to explain this more fully. The comment makes almost no sense. Python isn't a browser and won't render the HTML the way a browser would. Please provide some more detailed explanation of what you want to happen. – S.Lott Jul 09 '09 at 14:32
  • ok, i edited it. i don't want to render anything. i'm not sure if i'm approaching problem correctly, but i wrote list of what i want to happen - if i can achieve it differently feel free to comment. – Phil Jul 09 '09 at 14:47
  • @Phil: "it runs php script (that's part of code i'm asking about)" Do you want Python on your local box run some script a remote server? Is that what you're asking? – S.Lott Jul 09 '09 at 14:52
  • yes, exactly (hmmmmmmmmmm 15 characters) – Phil Jul 09 '09 at 14:56
  • @Phil: If you want to know how to run a remote process (PHP or anything else for that matter) please update your question to emphasize that you want to run a *remote* process on another host. Makes this crystal clear, if you can. Otherwise, you won't get any useful help. – S.Lott Jul 09 '09 at 17:00
  • Duplicate: http://stackoverflow.com/questions/923691/how-to-start-a-process-on-a-remote-server-disconnect-then-later-collect-output – S.Lott Jul 09 '09 at 17:01
  • Duplicate: http://stackoverflow.com/questions/946946/how-to-execute-a-process-remotely-using-python – S.Lott Jul 09 '09 at 17:01
  • "let's say located in the same dir" or it's on a remote server? Which is it? Please update the question to be correct and consistent. – S.Lott Jul 09 '09 at 18:23
  • I updated it. Note that I don't have shell access. I have ftp. – Phil Jul 09 '09 at 18:51

3 Answers3

5
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
  • 176,835
  • 32
  • 241
  • 292
4

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
  • 146,731
  • 54
  • 156
  • 201
  • 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 Jul 09 '09 at 18:54
0

I'll paraphrase the answer to How do I include a PHP script in Python?.

import subprocess

def php(script_path):
    p = subprocess.Popen(['php', script_path] )
Community
  • 1
  • 1
S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 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 Jul 09 '09 at 15:04