I’m running a python script from my webserver initiated by PHP. If the python script is simple, for example, print Hello World, it will execute flawlessly.
However, as soon as I add the playwright library and do anything, the server will not run the script…
Does anyone have a quick fix?
I am running test.php which is:
<?php
$command = escapeshellcmd('python3 test.py');
$output = shell_exec($command);
echo $output;
?>
If test.py =
print(5)
Then output equals "5"
If test.py =
from playwright.sync_api import Playwright, sync_playwright, expect
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)
Then there is no output. The script runs fine if I run it from the command line of the server.
I don’t know how to check what the error is, the script runs fine from command line and there is no php in the error log. I am running plesk
EDIT:
[![Running from the console][1]][1]Here I am running the following script from the console:
from playwright.sync_api import Playwright, sync_playwright, expect
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)
As seen, it runs and outputs the 5. However I know run it from the PHP script which is ran from a html page on the website.
Now changing the script to
print(5)
I run it from the console:
~~@~~:/var/www/vhosts/~~/httpdocs# python3 test.py
5
Censored for privacy reasons. I know run that script from the html page and:
[![enter image description here][2]][2]It outputs the 5.
This is pretty much everything I have. The php error log shows empty, the console does not return a error for both scripts. The only thing that changes is the usage of playwright
[1]: https://i.stack.imgur.com/nXiB8.png
[2]: https://i.stack.imgur.com/upfgg.png
1
Answers
Your code works fine for me on Ubuntu 22, without changing permissions.
I’ve removed the syntax error and simplified the code to a minimal, reproducible example. Try running this on your machine.
test.py
:test.php
:Terminal output showing permissions, relevant version information and the script working both when run directly as well as through a localhost server: