I have written a python which used to take values from command line and process the same. 5 variables are taken using raw_input() and in each case something is returned to screen.
I want this whole interaction to happen via php program which calls my python program. They are supposed to exchange variables more than one time.
I have read the possible solutions like running the python through php via shell and passing arguments. But I am not sure how once I start my python program I can simply keep on sending variables to it so that my python program reaches its logical end by getting variables from php.
you have to use an IPC mechanism like file, pipe, named pipe, shared memory,...
http://en.wikipedia.org/wiki/Inter-process_communication
You can generally communicate between languages by using common
language formats, and using stdin and stdout [pipe] to communicate
the data.
from: http://www.stackoverflow.com/questions/14047979/executing-python-script-in-php-and-exchanging-data-between-the-two
Related
I am trying to run a program using PHP and keep sending output to it. I've tried using exec() but as the documentation page says, it hangs, waiting for the process to return.
Is there something like exec() that would allow me to keep sending commands to a CLI application?
Please note that since the application is closed-source, I don't have the option of changing the application to look for a lock file or any other thing suggested as answers to similar questions.
You are looking for the proc_open command. This command runs a process connecting its standard input and output to file descriptors opened as pipes in your program. What you write the 0 descriptor is taken as input on stdin by the process, and what it outputs can be read by your program as of from a file. The example code in the linked php documentation should get you going.
However, if you are trying to communicate with mysql specifically, rather use the built in mysql functionality of php
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.
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.
i want to have an constantly opened program (wrote in c++ prefferably).
when the php script is acessed, it will be acessed with some variables which will be passed to the active program. then the program will make some calculations, and it will pass other variables back to the php script, which will be echoed (or they can be echoed from the program too, if it is possible). after the php script ends, the program must be active!
i know that there is the command exec, and i can run a program with those params(which can be variables), but i don't want that since the program must run even if there are no active php script at that time.
i hope you understood my problem.
You can use shared memory functions.
I would suggest using sockets to communicate between the C++ program and the PHP script. Therefore your C++ program would act as a server and the PHP script would connect to it with the Socket functions and localhost address. Then you could send your data between the two programs and when the PHP script ends, the C++ program would stay alive and wait for the next connection.
I have a scraper which scrape one site (Written in python). While scraping the site, that print lines which are about to write in CSV. Scraper has been written in Python and now I want to execute it via PHP code. My question is
how can I print each line which is getting printed by python code.
I have used exec function but it is none of my use and gives output after executing all the program. So;
Is it possible to get python output printed while it is getting executed via PHP.
If i understand it well, your python scraper output to a file and you want to "live" display the output via php. What about doing and loop in php in which you use filemtime to know whether or not the file has been updated? You might add a little sleep in order not to overload your server.
If your are using a web page, you may use AJAX to reload only the concerned part of the page at a regular interval.
Hoping this helps you.
Simple case
Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php
More involved case
If you want scraper to run on background and only connect to it vis php from time to time, you can either use some pub/sub toolkit or implement a small web server in the scraper that you can fetch result updates with fopen("https://localhost:port/...") or curl. Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...
I'd communicate using stdout instead of a file. Meaning the python script writes to stdout and the php script reads that.
Using proc_open you can control the python process from php and also read it's output.
Instead of using exec you could use passthru, that will output the data directly to the browser. http://php.net/manual/en/function.passthru.php
That should be enough to get the println from your script.
I think I have a fair idea of what you are saying put I am not too sure what you mean.
If you mean to say that everytime the python script does a print, you want the php code to output what was print?
If that is the case you could pass it as a POST DATA via HTTP. That is instead of printing in Python, you could send it to the PHP Script, which on receiving the data would print it.
I am not too sure if this is what you want though.
For proper communication you need to setup any medium, so you can use fifo, through it you can write string in python and read it with php.
For PHP fifo
http://php.net/manual/en/function.posix-mkfifo.php
For Python
http://www.doughellmann.com/PyMOTW/Queue/
Simply use system() instead of exec(). exec() saves all lines of stdout output of the external program into an array, but system() flushes stdout output "as it happens".
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?