Cheers
Vijay
Why not just write it all in Python?
--
Nick Stinemates (ni...@stinemates.org)
http://nick.stinemates.org
Write a commandline-app in python, that does the work for you. Invoke
that using php.
Or use something like pyphp - but I haven't used it, can't comment on
its usability/support etc.
Diez
If you are under Windows, you can:
- call Python's functions via Active-Scripting
- call a Python COM server (functions or properties)
For that, use Pywin32. And, in all cases, call functions can use
parameters.
--
@-salutations
Michel Claveau
A simple yet dangerous and rather rubbish solution (possibly more of a
hack than a real implementation) could be achieved by using a
technique described above:
<?php
echo exec('python foo.py');
?>
I would look into pyphp though. This method has so many issues
attached to it it's hardly worth bothering with.
I'm with Nick when I say why on earth are you needing to call Python
from within PHP as opposed to using only Python or only PHP?
Alex.
> vijay schrieb:
>> Hi
>> I have a python code performing some computation for me.I have a
>> html page which passes certain argumnets to a php page.This php page
>> needs to pass on the value to the Python class and get the result
>> back.
>> How do I go about this??
I do this quite a bit:
<?php
#***** GET OUTPUT OF PYTHON PROGRAM *****
$command = "/some/directory/report.py arg1 arg2 2>&1";
$p = popen($command, 'r');
$output = fread($p, 1024 * 1024);
print $output;
?>
** Posted from http://www.teranews.com **
What is rubbish about that - except from the obvious cleansing of input
variables that has to take place? Python has a whole module dedicated to
that rubbish, called subprocess.
> I would look into pyphp though. This method has so many issues
> attached to it it's hardly worth bothering with.
> I'm with Nick when I say why on earth are you needing to call Python
> from within PHP as opposed to using only Python or only PHP?
While I certainly prefer to use Python wherever I can, that does not
mean that there aren't cases where legacy systems or other constraints
make this impossible. If I have e.g. a type3-based website - "how on
earth" should I replace that with Python (without wasting a lot of time)?
Diez
I don't understand how the 2 are mutually exclusive?
You can have PHP and Python bindings installed on the same Apache
server, unless I'm mistaken?
> I don't understand how the 2 are mutually exclusive?
>
> You can have PHP and Python bindings installed on the same Apache
> server, unless I'm mistaken?
Not everyone have the luxury of having mod_python installed. It
depends on the host. On the other hand, mod_php will almost certainly
be installed on any Apache server.
What about having to set up & maintain (which might not even possible on
a cheap hoster) two configs for that - just for having a few lines of
python being run? And how do you go about session-state sharing and so
forth? After all the scipt might need to be access controlled based on
login state.
I don't say that there aren't options to run python more direct. I
argumented against a rather bold statement of Mr. alexelder:
"""
A simple yet dangerous and rather rubbish solution (possibly more of a
hack than a real implementation) could be achieved by using a
technique described above:
"""
Diez
> A simple yet dangerous and rather rubbish solution (possibly more of a
> hack than a real implementation) could be achieved by using a
> technique described above:
>
> <?php
> echo exec('python foo.py');
This will spawn a Python interpreter, and not be particularly
efficient. You could just as well have used CGI.
Thanks for pointing that out. I thought the warning before hand
could've suggested that this implementation wasn't the best. I'll be
more explicit in the future.
> If you are under Windows, you can:
> - call Python's functions via Active-Scripting
> - call a Python COM server (functions or properties)
>
> For that, use Pywin32. And, in all cases, call functions can use
> parameters.
This is perhaps the preferred solution if the web server is IIS and
not Apache.
I'm in a bit of a similar situation. I decided to use python to
solve problems where I could, in a more regimented fashion. For
instance, I have a set of functions in a script, table.py. After I
set up mod_python to handle requests to a single directory with
python, I can call this with:
<?php include("http://localhost/py/table/nodes"); ?>
embedded in the page. This is probably pretty hackish too, but at
least it doesn't spawn a new process, and I don't have to solve things
that aren't related to display with php.
Don't cry me the river, I was just asking about his situation.
If there's a specific problem with using python, then write it in PHP?!?
You mean opening a local-loop socket instead of a anonymous socket,
hogging at least another apache process and then possibly spawning
another process if the python-script is implemented as real CGI - not
fast_cgi or python - is the better solution? I doubt that. More wasteful
in all aspects, with small to any gain at all.
Unix uses pipes as IPC all the time. I fail to see why that is "rubbish".
Diez