从PHP运行Python脚本,直到我添加Playwright模块

wfveoks0  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(165)

我正在从我的web服务器上运行一个由PHP启动的python脚本。如果python脚本很简单,例如,print Hello World,它将完美地执行。
但是,只要我添加剧作家库并做任何事情,服务器就不会运行脚本...
有人有快速解决的办法吗?
我正在运行test.php,它是:

<?php

$command = escapeshellcmd('python3 test.py');
$output = shell_exec($command); 

echo $output;

?>

如果test.py=

print(5)

则输出等于“5”
如果test.py=

from playwright.sync_api import Playwright, sync_playwright, expect

downloadpath = "controlebestanden/" + 

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=True)
    context = browser.new_context()

    # Open new page
    page = context.new_page()

    # Go to 
    page.goto("https://inzamelhelden.nl/beheer/rapportageplus.php")

    browser.close()

with sync_playwright() as playwright:
        run(playwright)

print(5)

然后就没有输出了。如果我从服务器的命令行运行脚本,它会运行得很好。
我不知道如何检查错误是什么,脚本从命令行运行良好,错误日志中没有php.我正在运行plesk

zwghvu4y

zwghvu4y1#

您的代码在Ubuntu 22上运行良好,无需更改权限。
我已经删除了语法错误,并将代码简化为一个最小的,可重复的示例。
test.py

from playwright.sync_api import sync_playwright

with sync_playwright() as playwright:
    browser = playwright.chromium.launch()
    page = browser.new_page()
    page.goto("https://www.example.com")
    print(page.text_content("h1").strip())
    browser.close()

test.php

<?php
echo shell_exec(escapeshellcmd("python3 test.py"));
?>

终端输出显示权限、相关版本信息以及直接运行和通过本地主机服务器运行的脚本:

$ python3 -m pip freeze | grep play
playwright==1.28.0
$ python3 -V
Python 3.10.6
$ php -v
PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies
$ ls -l test*
-rw-rw-r-- 1 g2 g2  61 Apr  7 11:24 test.php
-rw-rw-r-- 1 g2 g2 265 Apr  7 11:23 test.py
$ php test.php
Example Domain
$ php -S localhost:8000 &
[1] 211749
$ [Fri Apr  7 11:57:58 2023] PHP 8.1.2-1ubuntu2.11 Development Server (http://localhost:8000) started
$ curl http://localhost:8000/test.php
[Fri Apr  7 11:58:10 2023] 127.0.0.1:39170 Accepted
[Fri Apr  7 11:58:14 2023] 127.0.0.1:39170 [200]: GET /test.php
Example Domain

[Fri Apr  7 11:58:14 2023] 127.0.0.1:39170 Closing

相关问题