Python call in PHP

I have a Python script. Recently I wrote that I call using the command line with some parameters. Now I want a very thin web interface to call this script locally on my Mac.

I don't want to go through a little problem installing mod_python or mod_wsgi on my Mac, so I'm just going to make system () or popen () from PHP to call a Python script.

Any better ideas? Thanks in advance!

+70
python php
03 Oct '08 at 13:44
source share
8 answers

Depending on what you are doing, system () or popen () may be perfect. Use system () if the Python script has no output, or if you want the output of the Python script to be passed directly to the browser. Use popen () if you want to write data to standard Python script input or read data from standard Python script output in php. popen () will allow you to read or write, but not both. If you want both, check proc_open () , but with two-way communication between programs, you need to be careful to avoid deadlocks, where each program is waiting for the other to do something.

If you want to pass the data provided by the user to a Python script, then the big thing to take care of is team injection. If you are not careful, your user may send you data such as "; evilcommand"; and force your program to execute arbitrary commands against your will.

escapeshellarg () and escapeshellcmd () can help with this, but personally I like to delete everything that is not a known good character, using something like

preg_replace('/[^a-zA-Z0-9]/', '', $str) 
+95
Oct 03 '08 at 14:40
source share

The backquote statement will also allow you to run python scripts using the same syntax above

In the python file named python.py:

 hello = "hello" world = "world" print hello + " " + world 

In the php file python.php:

 $python = `python python.py`; echo $python; 
+23
Sep 20 '13 at 16:01
source share

There is also a PHP extension: Pip - Python in PHP , which I have never tried, but it only mortgaged for this case

+15
03 Oct '08 at 20:13
source share

You can run a python script through php and output in a browser.

Basically you should call a python script like this:

 $command = "python /path/to/python_script.py 2>&1"; $pid = popen( $command,"r"); while( !feof( $pid ) ) { echo fread($pid, 256); flush(); ob_flush(); usleep(100000); } pclose($pid); 

Note: if you run any time.sleep () in your python code, it will not output the results in the browser.

To work with full codes, visit How to execute a python script from php and show the output in a browser

+11
Jul 22 '12 at 15:33
source share

I do such fast and dirty scripts all the time. It is often enough to have a CGI or PHP script that simply uses system / popen to call some external program.

Just be careful if your web server is open to the Internet as a whole. In this case, make sure you sanitize your GET / POST login to prevent attackers from running arbitrary commands on your computer.

+7
Oct 03 '08 at 14:40
source share

Your call_python_file.php should look like this:

 <?php $item='Everything is awesome!!'; $tmp = exec("py.py $item"); echo $tmp; ?> 

Executes a python script and displays the result in a browser. For now, in your python script, the variable (sys.argv [1:]) will list all your arguments. To display argv as a string for wherever your php pulls out, so if you want to make a text area:

 import sys list1 = ' '.join(sys.argv[1:]) def main(): print list1 if __name__ == '__main__': main() 
+2
Nov 01 '16 at 7:57
source share

Please note: if you use a virtual environment (as in shared hosting mode), you must configure your path in python, for example: /home/user/mypython/bin/python ./cgi-bin/test.py

0
Aug 09 '17 at 13:49 on
source share

If you want to execute your Python script in PHP, you need to execute this command in your PHP script:

 exec('your script python.py') 
-one
Jun 16 '16 at 23:03
source share



All Articles