Run (remote) php script from (local) python script - 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.

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.

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")

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] )

Related

Get running status of python program in PHP

Hi I am doing a project with Raspberry Pi. I make a python program which have an endless loop inside. I also make a PHP website which call to that python program, make it run in background by this way:
$call = "sudo python ../python/readver12.py > /dev/null 2>&1 &";
shell_exec($call);
Everything seem okay, but I don't know how to get the status of my python program is running in background or not, and make it available in my website with PHP ?
I guess there is many ways to do that:
Try get the logs from terminal
You can throw some logs at your terminal while running your endless python script, and capture it with PHP
https://docs.python.org/3/howto/logging.html
Write on files and read with it with php
Straight forward, you write in file with python and read with PHP
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
API REST On PHP website and python with cURL
You can use cURL inside your python script to comunicate with your endpoints in php and get the needed data.
cURL: http://pycurl.io/docs/latest/index.html
PHP Api Rest: https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html
I hope it helps
With shell_exec() PHP will wait to continue your script until the application you're calling exits and will return the output of that application as a string. When your application picks back up the child is already done (or you've hit the PHP time limit).
It sounds like you want to start the process and monitor it while it's running. For that look at proc_open() and proc_get_status().

Using php exec() on windows networked drive with no wait or timeout

I have searched extensively to solve the issue of opening an external program in phpdesktop without PHP waiting ultimately making PHP timeout.
I need to be able to launch the program with exec() and have the rest of the PHP code execute without waiting on the exec() command. I have tried multiple solutions. To make matters even more complicated the file that is being opened is on a networked drive. Here is what I have tried but has not worked
pclose(popen('start' .$File. '>NUL 2>NUL"', 'r')); This didn't work because the drive is on the network
exec($File); Doesn't work as it waits instead of executing the remainder of the code
system($File); Doesn't work. Same result as exec()
exec($File > /dev/null); Obviously doesnt work because php is on windows
The file being executed is a video file: mp4, avi or mkv. So it's opening the external video player file but like I said above PHP ultimately times out and gives an error after 30 seconds without executing the rest of the code. I just need PHP to ignore the program it opened and go on about its tasks. Any help would be greatly appreciated.
You could use something like "nircmd" which is a windows command-line utility to perform tasks and is a perfect fit for php-desktop. You can use the variety of "exec" actions "nircmd" has, so you could call whatever you want and immediately return to php. See the manual (.chm archive), under windows 7/10 you might have to "unblock" that help file (at file properties), to view the contents.
If 'phpdesktop' uses the built in web server in php (php -S) then it is as far as I know not possible to do so without having the exec call blocking, same with proc_open etc
I looked it up and indeed phpdesktop uses the builtin web server:
https://github.com/cztomczak/phpdesktop/blob/c00988f69348b73b6dee27bdf45d145b719e2a3d/phpdesktop-chrome/php_server.cpp
In theory proc_open should work, but it doesn't

Python script not running on web server

I am trying to call a python script from a php script. When I run the PHP script in the linux command prompt, the python gets executed properly. But when I run the php script on the web server, the python script isn't executed. Can anyone tell me why this is so and what I can do to correct it?
You can run it from linux command,because the system has python interpret.
So I think you should check whether the server support interpret python.
(i.e.some server may support asp instead of php for same reason)

running console app from php script

i start a linux console app from my php5 script, it starts ok but then termintates. I've tried using system(), shell_exec and tried starting as background process but to no avail it starts and then quits.
What i am trying to achieve is from a remote browser start a console app using a php5 script and then it should remain running (just as it would if i started it from a bash shell) , i then want to send commands (from a bash shell it would be keyboard strokes) to the console app from another set of php5 scripts. Hope its clear what i am trying to do.
If anyone could give some info on the best way about doing this, as i think i may have something fundamentally wrong.
I have a Debian Lenny box running apache.The console app is just a simple program that prints to stdout and reads from stdin.
How do you expect to send input to this app? Where is it listening for input?
It simply may only support interactive use, and exit as a result of that. Or, even simpler, it may terminate because it sees that is has no input (nothing piped in or nothing from some file) and since it's not connected to an interactive shell, it has nothing to do. There's no point in waiting for input from a user that doesn't have a way to interact w/ the application.
On every request, PHP starts up, compiles your script and executes it. After execution, the script exists. When the script exits, all of the resources it was using, including file handles, database handles, and pipes to other programs are terminated.
You're going to need to find another way to keep your program open and have PHP communicate with it. Otherwise, every request to your script is going to open a new copy of the program, and then both will exit when the PHP script is complete.
Unfortunately without knowing what the program is, it will be hard to offer suggestions on how to go about doing this.

Start local PHP script w/ local Python script

The Python program I'm writing needs to start a local PHP script outside of Python's process. The program also needs to pass params to the PHP script. So far this seems to start the script:
os.system( path_to_script_here param param )
However, I'm pretty certain that Python remains running until the PHP script is complete.
I've also looked at the various os.spawn methods and I'm not sure which would be appropriate for my case. Any ideas?
Thanks!
See: How to start a background process in Python?

Categories