Running a Python script from PHP

后端 未结 9 2287
鱼传尺愫
鱼传尺愫 2020-11-22 00:57

I\'m trying to run a Python script from PHP using the following command:

exec(\'/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2\');

H

9条回答
  •  清歌不尽
    2020-11-22 01:21

    In my case I needed to create a new folder in the www directory called scripts. Within scripts I added a new file called test.py.

    I then used sudo chown www-data:root scripts and sudo chown www-data:root test.py.

    Then I went to the new scripts directory and used sudo chmod +x test.py.

    My test.py file it looks like this. Note the different Python version:

    #!/usr/bin/env python3.5
    print("Hello World!")
    

    From php I now do this:

    $message = exec("/var/www/scripts/test.py 2>&1");
    print_r($message);
    

    And you should see: Hello World!

提交回复
热议问题