Calling Python code from inside php

V

vijay

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
 
N

Nick Stinemates

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

Why not just write it all in Python?
 
D

Diez B. Roggisch

vijay said:
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??

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
 
M

MC

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

alexelder

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

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

Diez B. Roggisch

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
 
N

Nick Stinemates

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

sturlamolden

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

Diez B. Roggisch

Nick said:
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
 
S

sturlamolden

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

alexelder

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

sturlamolden

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

Eric Wertman

A simple yet dangerous and rather rubbish solution (possibly more of a
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.
 
N

Nick Stinemates

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

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

Diez B. Roggisch

Eric said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top