import sys
sys.exit()
details from the sys module documentation:
sys.exit([arg])Exit from Python. This is implemented by raising the
SystemExitexception, so cleanup actions specified by finally clauses oftrystatements are honored, and it is possible to intercept the exit attempt at an outer level.The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to
stderrand results in an exit code of 1. In particular,sys.exit("some error message")is a quick way to exit a program when an error occurs.Since
exit()ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
Note that this is the 'nice' way to exit. @glyphtwistedmatrix below points out that if you want a 'hard exit', you can use os._exit(*errorcode*), though it's likely os-specific to some extent (it might not take an errorcode under windows, for example), and it definitely is less friendly since it doesn't let the interpreter do any cleanup before the process dies. On the other hand, it does kill the entire process, including all running threads, while sys.exit() (as it says in the docs) only exits if called from the main thread, with no other threads running.
import sys
sys.exit()
details from the sys module documentation:
sys.exit([arg])Exit from Python. This is implemented by raising the
SystemExitexception, so cleanup actions specified by finally clauses oftrystatements are honored, and it is possible to intercept the exit attempt at an outer level.The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to
stderrand results in an exit code of 1. In particular,sys.exit("some error message")is a quick way to exit a program when an error occurs.Since
exit()ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
Note that this is the 'nice' way to exit. @glyphtwistedmatrix below points out that if you want a 'hard exit', you can use os._exit(*errorcode*), though it's likely os-specific to some extent (it might not take an errorcode under windows, for example), and it definitely is less friendly since it doesn't let the interpreter do any cleanup before the process dies. On the other hand, it does kill the entire process, including all running threads, while sys.exit() (as it says in the docs) only exits if called from the main thread, with no other threads running.
A simple way to terminate a Python script early is to use the built-in quit() function. There is no need to import any library, and it is efficient and simple.
Example:
#do stuff
if this == that:
quit()
Videos
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 Python script's output to go directly to the browser. Use popen() if you want to write data to the Python script's standard input, or read data from the Python script's standard output in php. popen() will only let you read or write, but not both. If you want both, check out 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 user supplied data to the Python script, then the big thing to be careful about is command injection. If you aren't careful, your user could send you data like "; evilcommand ;" and make your program execute arbitrary commands against your will.
escapeshellarg() and escapeshellcmd() can help with this, but personally I like to remove everything that isn't a known good character, using something like
preg_replace('/[^a-zA-Z0-9]/', '', $str)
The shell_exec() operator will also allow you to run python scripts using similar syntax to above
In a python file called python.py:
hello = "hello"
world = "world"
print hello + " " + world
In a php file called python.php:
$python = shell_exec("python python.py");
echo $python;
I was working on something that requires me to run a Python script with some arguments from PHP itself, and complete some task and print some output. The thing is, I tried shell_exec() and few more things mentioned in SO to run python script. But, the issue is that PHP doesn't seem to wait for my python script to complete. I need something that'll wait till the Python is done with the job. Can someone suggest me something?
EDIT : Typo in "shell_exec()" name.
UPDATE : After I re-looked at my script this morning.... seems like python can't open file. So, the PHP might not have permissions for that script. So, went ahead and gave it the perms. I added "www-data" group to that python file and changed python file's perms to 777. It still didn't work. But, the main issue right now is that Python script can't seem to be executed via apache2->Php.
Thanks everyone for all the help and hints. Appreciate it!
UPDATE 2 : I fixed the permission issue with "www-data" and it's working. THANKS EVERYONE!
Will this work and I'd so how risky is it to future problems?
Yes it will work, and how risky it is depends on how good your implementation is. This is perfectly acceptable if done correctly. I have successfully integrated PHP and C, when PHP was simply too slow to do certain niche tasks in real time (IIRC, PHP is 7 times slower than its C counterpart).
The best implementation is to ignore the fact they are PHP/Python specifically, and just treat them as two separate languages.
Now, you need to pick some 'standard' way of interacting. HTTP is probably the easiest: everyone has a web server setup already so it's just a matter of putting the script online.
From here, you need to get away from the fact they are PHP/Python still and design your API without knowing that. For example, imagine you had a calculator:
<?php
print '<answer>' . ($_GET['one'] + $_GET['two']) . '</answer>';
?>
Then your API would be http://location/script.php?one=5&two=7. This is perfectly acceptable: the user of the API expects an answer within an <answer> tag (my example is a lazy mans XML, purely as a demonstration, but I think you should look into outputting JSON). This, is not:
<?php
print eval($_GET['expr']);
?>
Since your API would be http://location/script.php?expr=5+7. Even though it would result in the same answer (and, ignoring any major security flaws), the caller needs to know the implementation details of PHP (eval is a PHP function which will just evaluate its input as real PHP code). If they did 2^3, is that 2 xor 3 or 2 * 2 * 2 (2 to the power 3). This is a bad design: your inputs should be independent of the language your API is implemented in. On top of this, how do we distinguish between an error message and an answer? No magic "if the response is not a number", please.
Python side - you just need to make sure HTTP requests fail gracefully. If you can't connect to your script, perhaps try again, and if that fails, give up with a nice error message asking them to try again later. Don't just display them a Python stack trace.
You should also document your API as well as possible - this will save you a lot of time down the line. Say what each of the inputs should be, what ranges they should fall in, and give some good examples of how to use it.
API side - you should also try to fail quickly if something is wrong. If a variable is missing, incomplete, or invalid, print a message and exit as the first thing you do. Do not just run with the values, and then let them get back a PHP error message (remember, they should not know or care what language your API is implemented in), but should get a nice friendly You didn't give me a 'two' variable. What do you want me to add 'one' to?.
Jay's answer is correct that you can do this thing.
The normal way to describe this is that you'll be developing the python algorithm (function) into a "service" (standalone application) that your PHP app will "consume" (make calls to). This is a fine idea if the algorithm is complex enough that you want to think of it as its own program; sequestering your code into separate parts at various levels (separate objects, separate libraries, separate servers) is often a good idea, provided you have clean (defined) separation of concerns.
If you do go this route, HTTP(S) is a good option. The other easy synchronous option would be to use system calls, either to the python script directly or to a daemon wrapping the script. This may have performance advantages, and may be the fastest solution to write, but HTTPS is going to have big flexibility and security advantages.
If an asynchronous option will work I might suggest that. Your php app will leave a record of work to be done in some shared location (like the database), and your python app will be constantly polling* for work that needs to be done. Meanwhile, your php app will be checking for, and using the results of, finished work.
Asynchronous solutions like this are a little tricky to get right, but can be really nice once you have them.
(*If you're on a big cloud provider like AWS or Azure you may have better options than a poll.)
All that said, be mindful of the objections of your developers!
The decision to break one app into multiple apps shouldn't be taken lightly. There are a lot of reasons to split a system into simpler independent parts, but the fact that a contractor happened to write their piece in the "wrong" language isn't one of them.
Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.
In PHP use shell_exec function:
Execute command via shell and return the complete output as a string.
It returns the output from the executed command or NULL if an error occurred or the command produces no output.
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
Into Python file test.py, verify this text in first line: (see shebang explain):
#!/usr/bin/env python
If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment's $PATH. The alternative would be to hardcode something like #!/usr/bin/python; that's ok, but less flexible.
In Unix, an executable file that's meant to be interpreted can indicate what interpreter to use by having a #! at the start of the first line, followed by the interpreter (and any flags it may need).
If you're talking about other platforms, of course, this rule does not apply (but that "shebang line" does no harm, and will help if you ever copy that script to a platform with a Unix base, such as Linux, Mac, etc).
This applies when you run it in Unix by making it executable (chmod +x myscript.py) and then running it directly: ./myscript.py, rather than just python myscript.py
To make executable a file on unix-type platforms:
chmod +x myscript.py
Also Python file must have correct privileges (execution for user www-data / apache if PHP script runs in browser or curl)
and/or must be "executable". Also all commands into .py file must have correct privileges.
Taken from php manual:
Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.
I recommend using passthru and handling the output buffer directly:
ob_start();
passthru('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2');
$output = ob_get_clean();
Example code:
import subprocess
# if the script don't need output.
subprocess.call("php /path/to/your/script.php")
# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
You can simply execute the php executable from Python.
Edit: example for Python 3.5 and higher using subprocess.run:
import subprocess
result = subprocess.run(
['php', 'image.php'], # program and arguments
stdout=subprocess.PIPE, # capture stdout
check=True # raise exception if program fails
)
print(result.stdout) # result.stdout contains a byte-string