PHP calls Python script

Source: Internet
Author: User
Tags php script python script

The last time to do user feedback automatic translation, wrote a python script, the Japanese user feedback translated into Chinese, although the effect is possible, but other children do not know the Python shoes can not be used, so a Web service, so that other people can access the query through the Web. The use of the Apache service, the specific environment (LAMP) building is not detailed, the main share of PHP call Python script background running processing.

  1. How PHP calls external programs

  The first thing to solve is how PHP invokes the Python script, which calls the external program in PHP with two functions, system and exec.

System () Prototype: string system (String command [, int $return _var]).

The system function itself has the ability to print commands to perform output, meaning that the output in the program can be displayed in a PHP page. If the program executes successfully, the return value of system is the last line of the program output and False if execution fails. The second parameter is optional, used to get the status code after the command executes, 0 means that the external program was successfully called, and 1 indicates that the call failed.

<? PHP      Echo ("congratulations!\n");     $cmd system ("Python feedback.py 20141010",$ret);     Echo $ret  " );? >

EXEC () Prototype: string exec (String command [, string array [, int return_var]])

The exec () function, like system (), also executes the given command, but does not output the result, but instead returns the last line of the result. Although it returns only the last line of the command result, the second parameter array gives the complete result by appending the result line to the end of the array. The third parameter can be used to obtain the status code of the command execution only if the second parameter is specified.

<? PHP     exec ("Python feedback.py 20141010",$array,$ret);     Echo $array");     Echo $ret");? >

Due to the call of the Python translation script, may need to request a number of Baidu translation API, so occasionally it will take a long time, in order to prevent PHP page blocking or timeout, put this command in the background of the system execution. The solution is to redirect the output of the command to another file or stream, and the redirect ">>" is to empty the contents of the original file and write new content, ">" is to append new content to the end of the file.

<? PHP      system ("Python feedback.py 20141010 >/tmp/null &");    // system ("Python feedback.py 20141010 >> tmp.txt &");

2. How PHP knows the end of the Python script running in the background

  The first thing I do is, Python script to write a tmp.txt before running, and then loop in PHP to determine whether the Tmp.txt file generation, but the result is conceivable, PHP blocked ... So Ajax is used to make the Web page asynchronous updates. The checkresult.php script is the loop to determine whether the Tmp.txt file is generated, in order to determine whether the Python script runs the end.

<script type= "Text/javascript" >functionCheckresult () {varXMLHTTP; if(Window.XMLHttpRequest) {      //code for ie7+, Firefox, Chrome, Opera, Safarixmlhttp=NewXMLHttpRequest (); }    Else {      //code for IE6, IE5xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); } XMLHTTP. onreadystatechange=function() {        if(Xmlhttp.readystate==4 && xmlhttp.status==200) {document. getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext; }} XMLHTTP. Open ("GET", "checkresult.php?t=" + math.random (),true); XMLHTTP.send ();}</script> <body>...<p id= ' Txthint ' >...</body>

checkresult.php Script

<?PHP$timeflag=True;  while($timeflag) {        Sleep(3); if(file_exists("Tmp.txt")) {            $timeflag=False; $response= "Success!!!";  Break; }    }    Echo $response;?>

The final implementation of the results are quite impressive, this is my first time with Ajax, feel good ~

Resources:

1. PHP Document: http://php.net/manual/en/function.system.php

2. Ajax Tutorial: http://www.w3school.com.cn/ajax/

PHP calls Python script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.