Attach the test Python script first
testCsv.py#!/usr/bin/python3# -*- coding: utf-8 -*-import timeimport csvimport randomimport sysfilepath = ‘/Users/magicyou/Desktop/python/temp/‘# 生成csv文件def exportCsv(title,data): print(‘test‘) print(‘test2‘) time.sleep(10) fileName = time.strftime(‘%Y%m%d%H%M‘,time.localtime(time.time())) + ‘_‘+ str(int(round((time.time()) * 1000))) + ".csv" with open(filepath + fileName,"w",newline=‘‘,encoding=‘gb18030‘) as csvfile: writer = csv.writer(csvfile) # 先写入columns_name try: writer.writerow(title) writer.writerows(data) except: info = sys.exc_info() return {‘status‘:False, ‘msg‘:‘文件写入失败!‘} return {‘status‘:True, ‘fileName‘:hostName + fileName}if __name__ == "__main__": import time title = [‘序号‘,‘姓名‘,‘年龄‘,‘价值度‘,‘喜好‘] data = [[‘序号‘,‘姓名‘,‘年龄‘,‘价值度‘,‘喜好‘],[‘序号‘,‘姓名‘,‘年龄‘,‘价值度‘,‘喜好‘],[‘序号‘,‘姓名‘,‘年龄‘,‘价值度‘,‘喜好‘]] exportCsv(title,data)
1 Description of the system function
string system ( string $command [, int &$return_var ] )
As with the C version of the system () function, this function executes the command specified by the commands parameter and outputs the execution result.
If PHP is running in a server module, the system () function will also attempt to automatically refresh the output cache of the Web server after each line has finished outputting.
Parameters
Parameters |
Description |
Command |
The command to execute. |
Return_var |
If the Return_var parameter is supplied, the return state after execution of the external command is set to this variable. |
return value
Success returns the last line of the command output, and FALSE if it fails
# 案例$last_line = system(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, $output);print_r($last_line);echo $output;// 页面返回// test// test0
2 Exec Function description
string exec ( string $command [, array &$output [, int &$return_var ]] )
EXEC () executes commands specified by the command parameter
Parameters
Parameters |
Description |
Command |
The command to execute. |
Output |
If an output parameter is supplied, this array is populated with the outputs of the command execution, and each row of output fills an element in the array. The data in the array does not contain white space characters at the end of the line, such as \ n characters. Note that if some elements are already included in the array, the EXEC () function appends the contents to the end of the array. If you do not want to append at the end of the array, use the unset () function to reset the group before passing in the Exec () function |
Return_var |
If the Return_var parameter is supplied, the return state after execution of the external command is set to this variable. |
return value
The last line of the command execution result. If you need to get all the unprocessed output data, use the PassThru () function.
If you want to get the output of the command, make sure to use an output parameter.
# 案例$last_line = exec(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, $output,$status);print_r($last_line);var_dump($output);echo $status;# 页面返回// test// array(1) {// [0]=>// string(4) "test"// }// 0
3 Popen Function Description
resource popen ( string $command , string $mode )
Opens a pipeline that points to a process that is generated by the execution of a derived command command.
Parameters
Parameters |
Description |
Command |
The command to execute. |
Mode |
Necessary. Specifies the connection mode. Possible values: r: Read-only. W: Write Only (open and empty existing files or create a new file) |
return value
Returns the same file pointer that is returned by fopen (), except that it is one-way (read or write only) and must be closed with pclose (). This pointer can be used for fgets (), FGETSS (), and fwrite (). When the pattern is ' r ', the returned file pointer equals the command's STDOUT, and when the pattern is ' W ', the returned file pointer equals the STDIN of the command.
Returns FALSE if an error occurs.
# 案例$handle = popen(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, ‘r‘);print_r($handle);echo "‘$handle‘; " . gettype($handle) . "\n";$read = fread($handle, 2096);echo $read;pclose($handle);# 页面返回// Resource id #2// ‘Resource id #2‘; resource// test
4 PassThru Function Description
void passthru ( string $command [, int &$return_var ] )
Similar to the EXEC () function, the PassThru () function is also used to execute external commands (command). This function is used instead of the exec () or system () function when the executed Unix command outputs binary data and needs to be delivered directly to the browser. Often used to perform commands such as pbmplus that can output image streams directly. By setting Content-type to Image/gif, and then calling the Pbmplus program output GIF file, you can output the image directly from the PHP script to the browser.
Parameters
Parameters |
Description |
Command |
The command to execute. |
Return_var |
If the Return_var parameter is supplied, the return status of the Unix command is logged to this parameter. |
return value
no return value.
# 案例$returnVal = passthru(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘, $status);print_r($status);# 页面返回// test// 0
5 shell_exec Function Description
string shell_exec ( string $cmd )
Executes the command through the shell environment and returns the full output as a string.
Parameters
Parameters |
Description |
Command |
The command to execute. |
return value
The output of the command execution. If an error occurs during execution or the process does not produce output, NULL is returned.
Note:
When an error occurs during process execution, or if the process does not produce output, NULL is returned, so using this function cannot detect whether the process executed successfully by the return value. If you need to check the exit codes for process execution, use the EXEC () function.
# 案例$returnVal = shell_exec(‘/usr/local/bin/python3 /Desktop/python/python/testCsv.py‘);echo $returnVal;# 页面返回// test
Tip
All of the above cases are PHP waiting for the script to complete the return status or results, how can only call script unequal results?
The complete command is as follows:
The advantage of this command is that when the program errors, the error is recorded in Log_testpython.txt
python3 /Desktop/python/python/testCsv.py > /temp/test/log_testPython.txt 2>&1 &
Principle: The output of the program is redirected to a file or other output stream
You can also do this:
Of course, this will not get to the foot of the newspaper wrong information
python3 /Desktop/python/python/testCsv.py > /temp/test/null 2>&1 &或者python3 /Desktop/python/python/testCsv.py > /temp/test/null &
There are several common functions for PHP to execute system commands