0

This is my php code :

$output = shell_exec("python3 test.py");

echo $output;

This is my python code

#!/usr/bin/env python3
import requests
import sys


print("Hello from Python")

it show text Hello from Python when accessed from browser and also terminal using php test.php

When I change my python code to include import mysql.connector

#!/usr/bin/env python3
import requests
import mysql.connector
import sys


print("Hello from Python")

it show text Hello from Python when accessed from terminal using php test.php but not showing anything when accessed using my browser, why?

dramasea
  • 3,370
  • 16
  • 49
  • 77
  • What exactly do you mean "accessed using my browser"? How is that done? What exact address in the browser? – John Gordon May 14 '23 at 21:52
  • localhost/test.php – dramasea May 14 '23 at 22:09
  • 1
    as far as I recall additional python modules are installed per user (though I may be wrong). The terminal user is not the same as the user for the PHP process that runs in response to web requests – apokryfos May 14 '23 at 22:14
  • @apokryfos anyway to fix this? – dramasea May 14 '23 at 22:37
  • Possibly `sudo -u www-data pip install mysql` – apokryfos May 15 '23 at 05:23
  • Suggest reading this: https://stackoverflow.com/a/19736494/2067753 especially "*you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.*" btw: you probably need to identify the o/s (and version) you are using – Paul Maxwell May 15 '23 at 08:27

0 Answers0