skip to Main Content

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


  1. 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:

    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"));
    ?>
    

    Terminal output showing permissions, relevant version information and the script working both when run directly as well as through a localhost server:

    $ 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search