Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling Python code from inside php

53 views
Skip to first unread message

vijay

unread,
Apr 23, 2008, 2:09:53 PM4/23/08
to
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??


Cheers
Vijay

Nick Stinemates

unread,
Apr 23, 2008, 2:40:32 PM4/23/08
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list

Why not just write it all in Python?

--
Nick Stinemates (ni...@stinemates.org)
http://nick.stinemates.org

Diez B. Roggisch

unread,
Apr 23, 2008, 2:42:44 PM4/23/08
to
vijay schrieb:

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

MC

unread,
Apr 23, 2008, 3:08:10 PM4/23/08
to
Hi!

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


alex...@gmail.com

unread,
Apr 23, 2008, 3:13:40 PM4/23/08
to

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.

Tobiah

unread,
Apr 23, 2008, 3:17:06 PM4/23/08
to
On Wed, 23 Apr 2008 20:42:44 +0200, Diez B. Roggisch wrote:

> 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 **

Diez B. Roggisch

unread,
Apr 23, 2008, 3:35:43 PM4/23/08
to
> 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');
> ?>

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

Nick Stinemates

unread,
Apr 23, 2008, 11:51:08 PM4/23/08
to pytho...@python.org
> 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)?

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?

sturlamolden

unread,
Apr 24, 2008, 10:56:14 PM4/24/08
to
On Apr 24, 5:51 am, Nick Stinemates <n...@stinemates.org> wrote:

> 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.

Diez B. Roggisch

unread,
Apr 25, 2008, 9:29:49 AM4/25/08
to
Nick Stinemates schrieb:

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

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

sturlamolden

unread,
Apr 25, 2008, 11:02:01 AM4/25/08
to
On Apr 23, 9:13 pm, alexel...@gmail.com wrote:

> 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.

alex...@gmail.com

unread,
Apr 25, 2008, 1:37:23 PM4/25/08
to

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.

sturlamolden

unread,
Apr 25, 2008, 3:56:21 PM4/25/08
to
On Apr 23, 9:08 pm, MC <XX.X...@XX.XmclaveauX.com> wrote:

> 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.

Eric Wertman

unread,
Apr 25, 2008, 4:03:28 PM4/25/08
to sturlamolden, pytho...@python.org
> > 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.

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.

Nick Stinemates

unread,
Apr 25, 2008, 6:36:46 PM4/25/08
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list

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?!?

Diez B. Roggisch

unread,
Apr 26, 2008, 2:44:09 PM4/26/08
to
Eric Wertman schrieb:

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

0 new messages