Home Backend Development PHP Tutorial ​php调用python失败怎么办

​php调用python失败怎么办

Jun 02, 2016 am 11:29 AM
php python

​php调用python失败怎么办

php调用python失败怎么办?

假设有文件:php_test.php  python_test.py

在php文件中运行Python:

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

如果运行Python出错并不能保存在数组array中,因此应该把标准错误重定向到文件中,以上代码改写如下:

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

在bash中0,1,2三个数字分代表STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO,即标准输入(一般是键盘),标准输出(一般是显示屏,准确的说是用户终端控制台),标准错误(出错信息输出)。也可以通过以下方式将标准错误重定向到标准输出保存到$array中:

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

然后就可以根据错误信息去寻求解决办法。

一般在终端通过命令运行PHP文件是可以马上看到错误信息的,但是通过浏览器运行PHP文件就只能上面所述方法去输出错误信息。因此问题也就是在命令行下PHP可以成功执行Python文件,而通过浏览器就不能成功运行,这是因为两种方式所调用的动态库不一致!通过命令行方式调用的是系统中的已有的动态库,而通过浏览器方式调用的是Web服务器中的动态库。(我安装的XAMPP,所以通过浏览器方式调用的就是lampp/lib中的动态库)。

解决办法:

1.在输出的错误信息中找到出错的动态库

2.通过locate命令找到相关的动态库所在的位置:locate libxxx.so

3.将web服务器中的同名动态库删除或重命名

4.将通过第2步在系统中找到的动态库链接到web服务器的lib目录中

实例:

Python中使用hashlib模块时可能出现两个错误:

<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>
Copy after login

1.找出系统中包含libcrypto.so.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
Copy after login

2.将web服务器中的同名动态库重命名:sudo mv /opt/lampp/lib/libcryto.so.1.1 /opt/lampp/lib/libcryto.so.1.1.bak

3.将系统中libcryto.so.1.1链接到web服务器的lib目录中: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解决步骤同上。

更多相关知识,请访问PHP中文网

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

How to obtain real-time application and viewer data on the 58.com work page? How to obtain real-time application and viewer data on the 58.com work page? Apr 05, 2025 am 08:06 AM

How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

【Rust Self-study】Introduction 【Rust Self-study】Introduction Apr 04, 2025 am 08:03 AM

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

JavaScript code line break: How to handle long string and object attribute access gracefully? JavaScript code line break: How to handle long string and object attribute access gracefully? Apr 05, 2025 am 08:03 AM

Detailed explanation of JavaScript code line-breaking skills When writing JavaScript code, we often encounter a line of code that is too long, which not only affects the readability of the code...

See all articles