Home  >  Article  >  Backend Development  >  What to do if php fails to call python

What to do if php fails to call python

藏色散人
藏色散人Original
2019-12-13 14:33:534973browse

What to do if php fails to call python

What should I do if php fails to call python?

Solution to the failure of PHP calling Python

Assume there is a file: php_test.php python_test.py

Run Python in the php file :

exec("python python_test.py", $array, $ret);

If an error occurs while running Python, it cannot be saved in the array array, so the standard error should be redirected to the file. The above code is rewritten as follows:

exec("python python_test.py 2>error.txt", $array, $ret);

In bash 0,1, The three numbers 2 represent STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO, which are standard input (usually the keyboard), standard output (usually the display screen, or the user terminal console to be precise), and standard error (error information output).

You can also redirect standard error to standard output and save it in $array in the following way:

exec("python python_test.py 2>error.txt 2>&1", $array, $ret);

Then you can find a solution based on the error message.

Generally, you can see the error message immediately when you run a PHP file through a command on the terminal, but when you run a PHP file through a browser, you can only output the error message using the above method. Therefore, the problem is that PHP can successfully execute Python files under the command line, but cannot run successfully through the browser. This is because the dynamic libraries called by the two methods are inconsistent!

The existing dynamic library in the system is called through the command line, and the dynamic library in the Web server is called through the browser. (I installed XAMPP, so the dynamic library in lampp/lib is called through the browser).

Solution:

1. Find the wrong dynamic library in the output error message

2. Find the relevant dynamic library through the locate command Location: locate libxxx.so

3. Delete or rename the dynamic library with the same name in the web server

4. Link the dynamic library found in the system through step 2 to

Example in the lib directory of the web server:

Two errors may occur when using the hashlib module in Python:

<span style="font-family:Arial, Helvetica, sans-serif;">relocation error: python: symbol OpenSSL_add_all_digests, version OPENSSL_1.0.0 not defined in file libcrypto.so.1.0.0 with link time reference</span>
<span style="font-family:Arial, Helvetica, sans-serif;">python: /opt/lampp/lib/libcrypto.so.1.0.0: version `OPENSSL_1.0.2&#39; not found (required by /opt/lampp/lib/libssl.so.1.0.0)</span>

1. Find out that the system contains libcrypto.so. All paths to 1.0.0: locate libcryto.so.1.1

/home/ubuntu/.cache/vmware/drag_and_drop/52091a33-81b7-cc30-d88c-574c47558e32/ndk/libimobiledevice-android-master/openssl/libcrypto.so.1.0.0
/home/ubuntu/.cache/vmware/drag_and_drop/52091a33-81b7-cc30-d88c-574c47558e32/ndk/libimobiledevice-android-master/out/fsroot/lib/libcrypto.so.1.0.0
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0
/opt/lampp/lib/libcrypto.so.1.0.0

2. Rename the dynamic library with the same name in the web server:

sudo mv /opt/lampp/lib/libcryto.so.1.1 /opt/lampp/lib/libcryto.so.1.1.bak

3. Change libcryto.so.1.1 in the system Link to the lib directory of the web server:

sudo ln -s  /lib/x86_64-linux-gnu/libcrypto.so.1.0.0  /opt/lampp/lib/libcryto.so.1.1

libssl.so.1.0.0 The solution steps are the same as above.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of What to do if php fails to call python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why doesn't php parseNext article:Why doesn't php parse