PHP->bash->python 输出

发布于 2025-01-11 02:27:35 字数 3220 浏览 0 评论 0原文

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
passthru("bash /var/www/html/init.sh 2>&1",$pass_result);
echo $pass_result; 
?>

我无法在 PHP 级别获得 python 结果。 python 脚本 print("string") 到 bash 脚本,如果我

#!/bin/sh
START=$(python3 /usr/lib/cgi-bin/test.py)
echo "${START}"

这样做,如果我运行 bash 脚本,它会在终端上输出字符串 但如果我从 PHP 调用它,它会立即结束 错误:

#PHP OUTPUT
/usr/lib/python3/dist-packages/requests/__init__.py:89: 
RequestsDependencyWarning: urllib3 (1.26.0) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " Traceback (most recent call last): 
File "/usr/lib/cgi-bin/test.py", line 34, in driver = webdriver.Chrome(options=opts) 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 90, in __init__ self.service.start() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 98, in start self.assert_process_still_running() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 0

如果我在这段代码中放置一段时间和一个 for 行为变得很奇怪,

#!/bin/sh
START=python3 /usr/lib/cgi-bin/test.py &
BACK_PID=$!
while kill -0 $BACK_PID ; do
        for (( i=0; i<10; i++ )); do
            sleep 5
            echo "WTF" $i
        done
    echo "${OUTPUT}"
done

bash 脚本将在 for 循环的大约 8 次迭代中获得 python 字符串的输出,直接在终端上运行它,这是我得到的唯一方法PHP 等待 bash 脚本结束,并返回所有 9 WTF,但没有 python 字符串,我不明白为什么它没有到达 PHP。

#PHP OUTPUT
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.0) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " Traceback (most recent call last): 
File "/usr/lib/cgi-bin/test.py", line 34, in driver = webdriver.Chrome(options=opts) 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 90, in __init__ self.service.start() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 98, in start self.assert_process_still_running() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 WTF 0 WTF 1 WTF 2 WTF 3 WTF 4 WTF 5 WTF 6 WTF 7 WTF 8 WTF 9 /var/www/html/init.sh: line 4: kill: (11275) - No such process 0
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
passthru("bash /var/www/html/init.sh 2>&1",$pass_result);
echo $pass_result; 
?>

I am unable to get the python result at PHP level.
The python script print("string") to the bash script and if I do

#!/bin/sh
START=$(python3 /usr/lib/cgi-bin/test.py)
echo "${START}"

This works if I run the bash script, it outputs the string on the terminal
but if I call it from the PHP, it finishes immediately with this
error:

#PHP OUTPUT
/usr/lib/python3/dist-packages/requests/__init__.py:89: 
RequestsDependencyWarning: urllib3 (1.26.0) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " Traceback (most recent call last): 
File "/usr/lib/cgi-bin/test.py", line 34, in driver = webdriver.Chrome(options=opts) 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 90, in __init__ self.service.start() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 98, in start self.assert_process_still_running() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 0

If I put a while and a for the behavior becomes weird

#!/bin/sh
START=python3 /usr/lib/cgi-bin/test.py &
BACK_PID=$!
while kill -0 $BACK_PID ; do
        for (( i=0; i<10; i++ )); do
            sleep 5
            echo "WTF" $i
        done
    echo "${OUTPUT}"
done

with this code the bash script will get the output of the python string at around 8 iterations of the for loop running it directly on the terminal and this is the only way I got PHP to wait for the end of the bash script, and it returns all
9 WTFs but no python string, I can't understand why it is not reaching the PHP.

#PHP OUTPUT
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.0) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " Traceback (most recent call last): 
File "/usr/lib/cgi-bin/test.py", line 34, in driver = webdriver.Chrome(options=opts) 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__ super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog", 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 90, in __init__ self.service.start() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 98, in start self.assert_process_still_running() 
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1 WTF 0 WTF 1 WTF 2 WTF 3 WTF 4 WTF 5 WTF 6 WTF 7 WTF 8 WTF 9 /var/www/html/init.sh: line 4: kill: (11275) - No such process 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文